libname temp 'R:\Active\mcbs\SAS\data\temp'; /* save temporary datasets here */
*********************************************************************************************;

%LET YEAR=2016;
%LET YEARt1=2016;
%LET YR=1;

/* these are the paths to the mcbs survey data by year */
options nofmterr; /* run this so I can open the data without the irritating-ass formats */
libname svy_&YR.s "R:\Active\mcbs\&YEAR.\survey\Data\SAS Files"; 
libname cs&YR.s "R:\Active\mcbs\&YEAR.\cost\Data\SAS Files";

/* these are the paths to the mcbs claims data by year */
libname claims&YR. "R:\Active\mcbs\&YEAR.\survey\Data\Research Claims\SAS Files"; 


/* 1a - Setting up Study Population File - Insurance Details from Hitline */
data hitline_&YEAR. (keep = baseid va_mm prvsup_mm prvsup_med_mm prvsup_rx_mm pta_1--pta_12 ptb_1--ptb_12 ptc_1--ptc_12 ptd_1--ptd_12);
set svy_&YR.s.hitline;

if PLANTYPE=40 then va_mm = sum(of COV01-COV12); 
if PLANTYPE in (20,21,30,31,60,70) then prvsup_mm = sum(of COV01-COV12); 
if PLANTYPE in (20,21,30,31,70) and (S_INS=1 or S_MSCOV=1 or S_IP=1) then prvsup_med_mm = sum(of COV01-COV12); 
if (PLANTYPE in (20,21,30,31,60,70) and (S_INS=1 or S_INS=5 or D_COVRX=1)) or PLANTYPE=60 
then prvsup_rx_mm = sum(of COV01-COV12); 

if PLANTYPE=1 then pta_1 = COV01; 
if PLANTYPE=1 then pta_2 = COV02; 
if PLANTYPE=1 then pta_3 = COV03; 
if PLANTYPE=1 then pta_4 = COV04; 
if PLANTYPE=1 then pta_5 = COV05; 
if PLANTYPE=1 then pta_6 = COV06; 
if PLANTYPE=1 then pta_7 = COV07; 
if PLANTYPE=1 then pta_8 = COV08; 
if PLANTYPE=1 then pta_9 = COV09; 
if PLANTYPE=1 then pta_10 = COV10; 
if PLANTYPE=1 then pta_11 = COV11; 
if PLANTYPE=1 then pta_12 = COV12; 

if PLANTYPE=2 then ptb_1 = COV01; 
if PLANTYPE=2 then ptb_2 = COV02; 
if PLANTYPE=2 then ptb_3 = COV03; 
if PLANTYPE=2 then ptb_4 = COV04; 
if PLANTYPE=2 then ptb_5 = COV05; 
if PLANTYPE=2 then ptb_6 = COV06; 
if PLANTYPE=2 then ptb_7 = COV07; 
if PLANTYPE=2 then ptb_8 = COV08; 
if PLANTYPE=2 then ptb_9 = COV09; 
if PLANTYPE=2 then ptb_10 = COV10; 
if PLANTYPE=2 then ptb_11 = COV11; 
if PLANTYPE=2 then ptb_12 = COV12;

if PLANTYPE=3 then ptc_1 = COV01; 
if PLANTYPE=3 then ptc_2 = COV02; 
if PLANTYPE=3 then ptc_3 = COV03; 
if PLANTYPE=3 then ptc_4 = COV04; 
if PLANTYPE=3 then ptc_5 = COV05; 
if PLANTYPE=3 then ptc_6 = COV06; 
if PLANTYPE=3 then ptc_7 = COV07; 
if PLANTYPE=3 then ptc_8 = COV08; 
if PLANTYPE=3 then ptc_9 = COV09; 
if PLANTYPE=3 then ptc_10 = COV10; 
if PLANTYPE=3 then ptc_11 = COV11; 
if PLANTYPE=3 then ptc_12 = COV12; 

if PLANTYPE=4 then ptd_1 = COV01; 
if PLANTYPE=4 then ptd_2 = COV02; 
if PLANTYPE=4 then ptd_3 = COV03; 
if PLANTYPE=4 then ptd_4 = COV04; 
if PLANTYPE=4 then ptd_5 = COV05; 
if PLANTYPE=4 then ptd_6 = COV06; 
if PLANTYPE=4 then ptd_7 = COV07; 
if PLANTYPE=4 then ptd_8 = COV08; 
if PLANTYPE=4 then ptd_9 = COV09; 
if PLANTYPE=4 then ptd_10 = COV10; 
if PLANTYPE=4 then ptd_11 = COV11; 
if PLANTYPE=4 then ptd_12 = COV12; 

run;

proc sort data = hitline_&YEAR.; by baseid; run;

proc summary data = hitline_&YEAR.;
by baseid;
var va_mm prvsup_mm prvsup_med_mm prvsup_rx_mm pta_1--pta_12 ptb_1--ptb_12 ptc_1--ptc_12 ptd_1--ptd_12;
output out=hitline_&YEAR.(keep=baseid va_mm prvsup_mm prvsup_med_mm prvsup_rx_mm pta_1--pta_12 ptb_1--ptb_12 ptc_1--ptc_12 ptd_1--ptd_12)sum=;
run;

data hitline_&YEAR. (keep = BASEID CL_YR pta_mm ptb_mm ptd_1--ptd_12 FFS_MM MA_MM MA_FLAG PRVSUP PRVSUP_MED PRVSUP_RX VA_FLAG);
set hitline_&YEAR.;

CL_YR = &YEAR.;

if pta_1=1 and ptc_1 ne 1 then ffsa_1=1; else ffsa_1=0;
if pta_2=1 and ptc_2 ne 1 then ffsa_2=1; else ffsa_2=0;
if pta_3=1 and ptc_3 ne 1 then ffsa_3=1; else ffsa_3=0;
if pta_4=1 and ptc_4 ne 1 then ffsa_4=1; else ffsa_4=0;
if pta_5=1 and ptc_5 ne 1 then ffsa_5=1; else ffsa_5=0;
if pta_6=1 and ptc_6 ne 1 then ffsa_6=1; else ffsa_6=0;
if pta_7=1 and ptc_7 ne 1 then ffsa_7=1; else ffsa_7=0;
if pta_8=1 and ptc_8 ne 1 then ffsa_8=1; else ffsa_8=0;
if pta_9=1 and ptc_9 ne 1 then ffsa_9=1; else ffsa_9=0;
if pta_10=1 and ptc_10 ne 1 then ffsa_10=1; else ffsa_10=0;
if pta_11=1 and ptc_11 ne 1 then ffsa_11=1; else ffsa_11=0;
if pta_12=1 and ptc_12 ne 1 then ffsa_12=1; else ffsa_12=0;
PTA_MM = sum(of ffsa_1-ffsa_12); 

if ptb_1=1 and ptc_1 ne 1 then ffsb_1=1; else ffsb_1=0;
if ptb_2=1 and ptc_2 ne 1 then ffsb_2=1; else ffsb_2=0;
if ptb_3=1 and ptc_3 ne 1 then ffsb_3=1; else ffsb_3=0;
if ptb_4=1 and ptc_4 ne 1 then ffsb_4=1; else ffsb_4=0;
if ptb_5=1 and ptc_5 ne 1 then ffsb_5=1; else ffsb_5=0;
if ptb_6=1 and ptc_6 ne 1 then ffsb_6=1; else ffsb_6=0;
if ptb_7=1 and ptc_7 ne 1 then ffsb_7=1; else ffsb_7=0;
if ptb_8=1 and ptc_8 ne 1 then ffsb_8=1; else ffsb_8=0;
if ptb_9=1 and ptc_9 ne 1 then ffsb_9=1; else ffsb_9=0;
if ptb_10=1 and ptc_10 ne 1 then ffsb_10=1; else ffsb_10=0;
if ptb_11=1 and ptc_11 ne 1 then ffsb_11=1; else ffsb_11=0;
if ptb_12=1 and ptc_12 ne 1 then ffsb_12=1; else ffsb_12=0;
PTB_MM = sum(of ffsb_1-ffsb_12); 

if pta_1=1 and ptb_1=1 and ptc_1 ne 1 then ffs_1=1; else ffs_1=0;
if pta_2=1 and ptb_2=1 and ptc_2 ne 1 then ffs_2=1; else ffs_2=0;
if pta_3=1 and ptb_3=1 and ptc_3 ne 1 then ffs_3=1; else ffs_3=0;
if pta_4=1 and ptb_4=1 and ptc_4 ne 1 then ffs_4=1; else ffs_4=0;
if pta_5=1 and ptb_5=1 and ptc_5 ne 1 then ffs_5=1; else ffs_5=0;
if pta_6=1 and ptb_6=1 and ptc_6 ne 1 then ffs_6=1; else ffs_6=0;
if pta_7=1 and ptb_7=1 and ptc_7 ne 1 then ffs_7=1; else ffs_7=0;
if pta_8=1 and ptb_8=1 and ptc_8 ne 1 then ffs_8=1; else ffs_8=0;
if pta_9=1 and ptb_9=1 and ptc_9 ne 1 then ffs_9=1; else ffs_9=0;
if pta_10=1 and ptb_10=1 and ptc_10 ne 1 then ffs_10=1; else ffs_10=0;
if pta_11=1 and ptb_11=1 and ptc_11 ne 1 then ffs_11=1; else ffs_11=0;
if pta_12=1 and ptb_12=1 and ptc_12 ne 1 then ffs_12=1; else ffs_12=0;
FFS_MM = sum(of ffs_1-ffs_12); 

MA_MM = sum(of ptc_1-ptc_12); 
if MA_MM >=1 then ma_flag =1; else ma_flag = 0;
if prvsup_mm >=1 then prvsup=1; else prvsup=0;
if prvsup_med_mm >=1 then prvsup_med=1; else prvsup_med=0;
if prvsup_rx_mm >=1 then prvsup_rx=1; else prvsup_rx=0;
if va_mm >=1 then va_flag=1; else va_flag=0;
run;

/* 1b - Setting up Study Population File - Insurance Details from Hisummary*/
data hisumry_&YEAR. (Keep = BASEID H_MEDSTA H_PDLS01--H_PDLS12 NEW_BENE ESRD DISABLED OREC_AGE_ONLY MCAID_ANY MCAID_FULL MCAID_PART MC_ENT_ST MCR_ENT_YR MCR_ENT_MM SHSV_ACO1 NXGN_ACO CPCPL_PCMH);
set svy_&YR.s.hisumry;

mc_st = year(H_DOE);
if mc_st = &YEAR. then NEW_BENE = 1; else NEW_BENE = 0;

If H_MEDSTA in ('11','21','31') then ESRD = 1; else ESRD=0;
if H_MEDSTA in ('20','21') then DISABLED = 1; else DISABLED=0;
if H_MEDSTA in ('10','11') then OREC_AGE_ONLY = 1; else OREC_AGE_ONLY=0;

if H_OPMDCD in (1,3,4) then MCAID_ANY=1; else MCAID_ANY=0;
if H_DUAL01 in ('02','04','08') or H_DUAL02 in ('02','04','08') or H_DUAL03 in ('02','04','08') or H_DUAL04 in ('02','04','08') or H_DUAL05 in ('02','04','08') or
H_DUAL06 in ('02','04','08') or H_DUAL07 in ('02','04','08') or H_DUAL08 in ('02','04','08') or H_DUAL09 in ('02','04','08') or H_DUAL10 in ('02','04','08') or
H_DUAL11 in ('02','04','08') or H_DUAL12 in ('02','04','08') then MCAID_FULL=1; else MCAID_FULL=0;
if MCAID_ANY=1 and MCAID_FULL=0 then MCAID_PART=1; else MCAID_PART=0;

MC_ENT_ST=H_DOE; format MC_ENT_ST yymmdd10.; if MC_ENT_ST < (MDY(01,01,&YEAR.)) then MC_ENT_ST_YRMNDY=(MDY(01,01,&YEAR.)); else MC_ENT_ST_YRMNDY=MC_ENT_ST; /* medicare entitlement start date in year */
MC_ENT_ED_YRMNDY=H_DOT; format MC_ENT_ED_YRMNDY yymmdd10.; if MC_ENT_ED_YRMNDY=. then MC_ENT_ED_YRMNDY=(MDY(12,31,&YEAR.)); /* medicare entitlement end date in year */
MCR_ENT_MM=intck('month', MC_ENT_ST_YRMNDY, MC_ENT_ED_YRMNDY) +1; /* Medicare entitlement coverage months */ 
MCR_ENT_YR = (MDY(01,01,&YEAR.) - MC_ENT_ST) / 365.25; /* Years with Medicare entitlement at the beginning of the year */

if H_PRGID = '08' or H_PRGID2 = '08' then SHSV_ACO1=1; else SHSV_ACO1=0;
if H_PRGID = '21' or H_PRGID2 = '21' then NXGN_ACO=1; else NXGN_ACO=0;
if H_PRGID in ('22','23') or H_PRGID2 in ('22','23') or H_PRGID3 ='23' then CPCPL_PCMH=1; else CPCPL_PCMH=0;

run;

proc sort data = hisumry_&YEAR.; by baseid; run;

/* 1c - Merging on MA Plan Data to Contract and Plan IDs from Hisummary*/
data maplan_&YEAR. (Keep = BASEID MA_CNT_ID MA_PLN_ID ma_ind);
set svy_&YR.s.hisumry;

if H_PRTC01 not in ('N','0') then MA_CNT_ID=H_PRTC01;
else if H_PRTC01 in ('N','0') then MA_CNT_ID=H_PRTC02;
else if MA_CNT_ID in ('N','0') then MA_CNT_ID=H_PRTC03;
else if MA_CNT_ID in ('N','0') then MA_CNT_ID=H_PRTC04;
else if MA_CNT_ID in ('N','0') then MA_CNT_ID=H_PRTC05;
else if MA_CNT_ID in ('N','0') then MA_CNT_ID=H_PRTC06;
else if MA_CNT_ID in ('N','0') then MA_CNT_ID=H_PRTC07;
else if MA_CNT_ID in ('N','0') then MA_CNT_ID=H_PRTC08;
else if MA_CNT_ID in ('N','0') then MA_CNT_ID=H_PRTC09;
else if MA_CNT_ID in ('N','0') then MA_CNT_ID=H_PRTC10;
else if MA_CNT_ID in ('N','0') then MA_CNT_ID=H_PRTC11;
else if MA_CNT_ID in ('N','0') then MA_CNT_ID=H_PRTC12;
if MA_CNT_ID in ('N','0') then MA_CNT_ID='';

if H_CPBP01 ne '' then MA_PLN_ID=H_CPBP01;
else if H_CPBP01='' then MA_PLN_ID=H_CPBP02;
else if MA_PLN_ID='' then MA_PLN_ID=H_CPBP03;
else if MA_PLN_ID='' then MA_PLN_ID=H_CPBP04;
else if MA_PLN_ID='' then MA_PLN_ID=H_CPBP05;
else if MA_PLN_ID='' then MA_PLN_ID=H_CPBP06;
else if MA_PLN_ID='' then MA_PLN_ID=H_CPBP07;
else if MA_PLN_ID='' then MA_PLN_ID=H_CPBP08;
else if MA_PLN_ID='' then MA_PLN_ID=H_CPBP09;
else if MA_PLN_ID='' then MA_PLN_ID=H_CPBP10;
else if MA_PLN_ID='' then MA_PLN_ID=H_CPBP11;
else if MA_PLN_ID='' then MA_PLN_ID=H_CPBP12;

if MA_CNT_ID ne '' then ma_ind=1; 
if ma_ind=1; /* only keeping those with MA plan contract IDs */

run; 

proc sort data = maplan_&YEAR.; by MA_CNT_ID; run;

/* merging in MA plan contract ID parent insurer - coded manually by me but originally from
https://www.cms.gov/Medicare/Prescription-Drug-Coverage/PrescriptionDrugCovGenIn/PerformanceData */

Data ptc_insurer_&YEAR.;
	INFILE "R:\Active\mcbs\SAS\ma_parentinsurer_2015_2019.txt" 
             DLM = '09'X DSD TRUNCOVER FIRSTOBS = 2;
     INPUT ma_cnt_id :$5. ma_ins_nm :$3.;
run;

proc sort data = ptc_insurer_&YEAR.; by ma_cnt_id; run;

data maplan_&YEAR.;
merge maplan_&YEAR. ptc_insurer_&YEAR.;
by ma_cnt_id;
if ma_ind=1;
run;

/* merging in MA plan star ratings at the contract ID level from CMS 
https://www.cms.gov/Medicare/Prescription-Drug-Coverage/PrescriptionDrugCovGenIn/PerformanceData */

Data ptc_stars_&YEAR.;
	INFILE "R:\Active\mcbs\SAS\ptc_stars_&YEAR._04.txt" 
             DLM = '09'X DSD TRUNCOVER FIRSTOBS = 2;
     INPUT ma_cnt_id :$5. ptc_str_rt :3.;
	 if ptc_str_rt ne .;
Run;

proc sort data = ptc_stars_&YEAR.; by ma_cnt_id; run;

data maplan_&YEAR.;
merge maplan_&YEAR. ptc_stars_&YEAR.;
by ma_cnt_id;
if ma_ind=1;
run;

proc sort data = maplan_&YEAR.; by MA_CNT_ID MA_PLN_ID; run;

/* merging in MA SNP indicators at the contract and plan ID level from CMS 
https://www.cms.gov/Research-Statistics-Data-and-Systems/Statistics-Trends-and-Reports/MCRAdvPartDEnrolData/Special-Needs-Plan-SNP-Data */

Data ma_snp_&YEAR.;
	INFILE "R:\Active\mcbs\SAS\masnp_&YEAR._01.txt" 
             DLM = '09'X DSD TRUNCOVER FIRSTOBS = 2;
     INPUT ma_cnt_id :$5. ma_pln_id :$3. snp_tp :$2. fide :1.;
	 ma_snp=1;
Run;

proc sort data = ma_snp_&YEAR. nodupkey; by ma_cnt_id ma_pln_id; run;

data maplan_&YEAR. (keep = baseid ma_ins_nm ptc_str_rt ma_snp ma_snp_cd ma_snp_de ma_snp_fide ma_snp_in);
merge maplan_&YEAR. ma_snp_&YEAR.;
by ma_cnt_id ma_pln_id;
if ma_ind=1;
if ma_snp=. then ma_snp=0;
if snp_tp='CD' then ma_snp_cd=1; else ma_snp_cd=0;
if snp_tp='DE' then ma_snp_de=1; else ma_snp_de=0;
if snp_tp='DE' and fide=1 then ma_snp_fide=1; else ma_snp_fide=0;
if snp_tp='IN' then ma_snp_in=1; else ma_snp_in=0;
run;

proc sort data = maplan_&YEAR.; by baseid; run;

/* 1 - Setting up Study Population File */
data sample_&YEAR. (keep = BASEID CL_YR PTA_MM PTB_MM FFS_MM MA_MM MA_FLAG PRVSUP PRVSUP_MED PRVSUP_RX VA_FLAG H_MEDSTA NEW_BENE ESRD DISABLED OREC_AGE_ONLY MCAID_ANY MCAID_FULL MCAID_PART MC_ENT_ST MCR_ENT_YR MCR_ENT_MM SHSV_ACO1 NXGN_ACO CPCPL_PCMH PTD_MM 
						PD_FLAG PARTD_LIS HASPTD HASPTB HASHMO MA_INS_NM PTC_STR_RT MA_SNP MA_SNP_CD MA_SNP_DE MA_SNP_FIDE MA_SNP_IN);
merge hitline_&YEAR. hisumry_&YEAR. maplan_&YEAR.;
by baseid;

if CL_YR = &YEAR.;

if MA_MM=. then MA_MM=0;
PTD_MM= sum(of ptd_1-ptd_12); if PTD_MM=. then PTD_MM=0;
if sum(of ptd_1-ptd_12) >=1 then pd_flag =1; else pd_flag = 0; /* indicates at least 1 partD enrollment month during year */
if ptd_1=1 and H_PDLS01 in ('01','02','03','04','05','06','07','08') then ptd_lis_1=1; else ptd_lis_1=0;
if ptd_2=1 and H_PDLS02 in ('01','02','03','04','05','06','07','08') then ptd_lis_2=1; else ptd_lis_2=0;
if ptd_3=1 and H_PDLS03 in ('01','02','03','04','05','06','07','08') then ptd_lis_3=1; else ptd_lis_3=0;
if ptd_4=1 and H_PDLS04 in ('01','02','03','04','05','06','07','08') then ptd_lis_4=1; else ptd_lis_4=0;
if ptd_5=1 and H_PDLS05 in ('01','02','03','04','05','06','07','08') then ptd_lis_5=1; else ptd_lis_5=0;
if ptd_6=1 and H_PDLS06 in ('01','02','03','04','05','06','07','08') then ptd_lis_6=1; else ptd_lis_6=0;
if ptd_7=1 and H_PDLS07 in ('01','02','03','04','05','06','07','08') then ptd_lis_7=1; else ptd_lis_7=0;
if ptd_8=1 and H_PDLS08 in ('01','02','03','04','05','06','07','08') then ptd_lis_8=1; else ptd_lis_8=0;
if ptd_9=1 and H_PDLS09 in ('01','02','03','04','05','06','07','08') then ptd_lis_9=1; else ptd_lis_9=0;
if ptd_10=1 and H_PDLS10 in ('01','02','03','04','05','06','07','08') then ptd_lis_10=1; else ptd_lis_10=0;
if ptd_11=1 and H_PDLS11 in ('01','02','03','04','05','06','07','08') then ptd_lis_11=1; else ptd_lis_11=0;
if ptd_12=1 and H_PDLS12 in ('01','02','03','04','05','06','07','08') then ptd_lis_12=1; else ptd_lis_12=0;
if sum(of ptd_lis_1-ptd_lis_12) >=1 then PARTD_LIS =1; else PARTD_LIS = 0; /* indicates at least 1 partD enrollment month with a PartD subsidy during year */
if pd_flag = 1 and PARTD_LIS = 0 then HASPTD = 1; else HASPTD = 0; /* indicates partD enrollment during the year but no PartD subsidy */

if ffs_mm >=1 and (ffs_mm=MCR_ENT_MM) then HASPTB=1; else HASPTB=0; /* indicates Part A+B coverage for the entire time beneficiary is entitled to Medicare during year */
if MA_FLAG=1 then HASHMO=1; else HASHMO=0;

run;

/* 2 - getting demographic variables */
data demo_&YEAR. (keep = BASEID H_DOB DOB AGE_0101 DIED DOD_YRMNDY AGE64 AGE74 AGE84 AGE85 USRD MBRZP MBRCTY MBRST SSA_SCTY RUCA1 ruca_metro1 ruca_micro1 ruca_rural1 ADI_SCR NEWENGLAND
						 MIDWEST WESTNORTHCENTRAL SOUTHATLANTIC EASTSCENTRAL WESTSCENTRAL MOUNTAIN PACIFIC MALE FEMALE WHITE BLACK HISPANIC ASIAN NTVAM HAWPAC MIXED OTHER OTHR_LANG ENG_SPK_PRB EDUNKN EDNOHS 
						 EDHSGD EDSMCL EDCLGD INCOME FPL_LTE100 FPL_101_200 FPL_GT200 MARRIED WIDOW DIV_SEP NEV_MARRY VET_STS WORKING_SR NE MW SO WE);
set svy_&YR.s.demo;

DOB = H_DOB; format DOB mmddyy10.; AGE_0101 = ROUND(((MDY(01,01,&YEAR.) - DOB)/365.25),0.01); 
if H_DOD ne . then died =1; else died = 0; 
DOD_YRMNDY = H_DOD; format DOD_YRMNDY yymmdd10.;

if age_0101 <65 then AGE64=1; else AGE64=0;
if age_0101 >=65 and age_0101 <75 then AGE74=1; else AGE74=0;
if age_0101 >=75 and age_0101 <85 then AGE84=1; else AGE84=0;
if age_0101 >=85 then AGE85=1; else AGE85=0;

if H_CENSUS in ('01','02','03','04','05','06','07','08','09') and H_ZIP ne '' then USRD = 1; else USRD = 0;
MBRZP=input(H_ZIP, 8.);
MBRCTY=H_RESCTY;
MBRST=H_RESST;
SSA_SCTY=trim(mbrst||mbrcty); /*ssa state-county code */ format ssa_scty $5.;

RUCA1=H_RUCA2;
if H_RUCA2 in ('1','1.1','2','2.1','3') then ruca_metro1 = 1; else ruca_metro1 = 0;
if H_RUCA2 in ('4','4.1','5','5.1','6') then ruca_micro1 = 1; else ruca_micro1 = 0;
if H_RUCA2 in ('7','7.1','7.2','8','8.1','8.2','9','10','10.1','10.2','10.3') then ruca_rural1 = 1; else ruca_rural1 = 0;

ADI_SCR=CENSADI; if ADI_SCR=. then ADI_SCR=ZIPADI; if ADI_SCR=. then ADI_SCR=ADINATNL; 

if H_CENSUS ='01' then NEWENGLAND=1; else NEWENGLAND=0;
if H_CENSUS ='03' then MIDWEST=1; else MIDWEST=0;
if H_CENSUS ='04' then WESTNORTHCENTRAL=1; else WESTNORTHCENTRAL=0;
if H_CENSUS ='05' then SOUTHATLANTIC=1; else SOUTHATLANTIC=0;
if H_CENSUS ='06' then EASTSCENTRAL=1; else EASTSCENTRAL=0;
if H_CENSUS ='07' then WESTSCENTRAL=1; else WESTSCENTRAL=0;
if H_CENSUS ='08' then MOUNTAIN=1; else MOUNTAIN=0;
if H_CENSUS ='09' then PACIFIC=1; else PACIFIC=0;

if H_CENSUS in ('01','02') then NE = 1; else NE = 0;
if H_CENSUS in ('03','04') then MW = 1; else MW = 0;
if H_CENSUS in ('05','06','07') then SO = 1; else SO = 0;
if H_CENSUS in ('08','09') then WE = 1; else WE = 0;

if H_SEX = '1' then MALE = 1; else MALE = 0;
if H_SEX = '2' then FEMALE = 1; else FEMALE = 0;
if D_RACE2 = '4' then WHITE =1; else WHITE =0;
if D_RACE2 = '2' then BLACK =1; else BLACK =0;
if HISPORIG = '1' then HISPANIC =1; else HISPANIC =0;
if D_RACE2 = '1' then ASIAN =1; else ASIAN =0;
if D_RACE2 = '5' then NTVAM =1; else NTVAM =0;
if D_RACE2 = '3' then HAWPAC =1; else HAWPAC =0;
if D_RACE2 = '7' then MIXED =1; else MIXED =0;
if D_RACE2 in ('D','N','R','.') then OTHER =1; else OTHER =0; /* unknown, refused, not ascertained, missing */
if OTHRLANG=1 then OTHR_LANG=1; else OTHR_LANG=0;
if ENGWELL in (3,4) then ENG_SPK_PRB=1; else ENG_SPK_PRB=0;

if SPDEGRCV not in (1,2,3,4,5,6,7,8,9) then EDUNKN =1; else if SPDEGRCV ne . then EDUNKN =0;
if SPDEGRCV in (1,2,3) then EDNOHS =1; else if SPDEGRCV ne . then EDNOHS =0;
if SPDEGRCV=4 then EDHSGD =1; else if SPDEGRCV ne . then EDHSGD =0;
if SPDEGRCV in (5,6,7) then EDSMCL =1; else if SPDEGRCV ne . then EDSMCL =0;
if SPDEGRCV in (8,9) then EDCLGD =1; else if SPDEGRCV ne . then EDCLGD =0;
INCOME = INCOME_H;
if IPR_IND=1 then FPL_LTE100=1; else FPL_LTE100=0;
if IPR_IND in (2,3,4) then FPL_101_200=1; else FPL_101_200=0;
if IPR_IND=5 then FPL_GT200=1; else FPL_GT200=0;

if SPMARSTA=1 then MARRIED=1; else if SPMARSTA ne . then MARRIED=0;
if SPMARSTA=2 then WIDOW=1; else if SPMARSTA ne . then WIDOW=0;
if SPMARSTA in (3,4) then DIV_SEP=1; else if SPMARSTA ne . then DIV_SEP=0;
if SPMARSTA=5 then NEV_MARRY=1; else if SPMARSTA ne . then NEV_MARRY=0;

if SPAFEVER=1 then VET_STS=1; else VET_STS=0;

if JOBSTAT=1 then WORKING_SR=1; else WORKING_SR=0;

run;

/* Identifying benes institutionaized in LTC facilities at any time during the year using the MDS3 and RESTMLN files */
data rest_inst_&YEAR. (keep = baseid MCBS_INST); /* this is a survey file generated by MCBS */
set svy_&YR.s.restmln;
If D_TYPE in ('B','F'); /* partial and full-year indicator for LTC inst days */
MCBS_INST=1;  
run;

proc sort data = rest_inst_&YEAR. nodupkey; by baseid; run;

data mds3_inst_&YEAR. (keep = BASEID MDS3_INST); /* this is a claims file generated by CMS */
set svy_&YR.s.mds3;
if A1600=. then entry_dt=MDY(01,01,&YEAR.); else entry_dt=A1600; format entry_dt mmddyy8.;
if A2000=. then disch_dt=MDY(12,31,&YEAR.); else disch_dt=A2000; format disch_dt mmddyy8.;
If A0200 ='1' 
and TRGT_DT ge (MDY(01,01,&YEAR.)) and TRGT_DT le (MDY(12,31,&YEAR.)) /* assessment at least once during year */
and entry_dt le (MDY(12,31,&YEAR.)) /* entered on or before last day of year */
and disch_dt ge (MDY(01,01,&YEAR.)) /* discharged on or after first day of year */
then MDS3_INST=1;
if MDS3_INST=1;
run;

proc sort data = mds3_inst_&YEAR. nodupkey; by baseid; run;

data inst_&YEAR. (keep = baseid MCBS_INST MDS3_INST);
merge rest_inst_&YEAR. mds3_inst_&YEAR.;
by baseid;
if MCBS_INST=1 or MDS3_INST=1 then INST=1;
if MCBS_INST=1 and MDS3_INST=1 then agree=1; else agree=0;
if MCBS_INST=1 and MDS3_INST ne 1 then mcbs_only=1; else mcbs_only=0;
if MCBS_INST ne 1 and MDS3_INST=1 then mds3_only=1; else mds3_only=0;
run;

data sample_&YEAR. (keep = BASEID CL_YR PTA_MM PTB_MM FFS_MM MA_MM MA_FLAG PRVSUP PRVSUP_MED PRVSUP_RX VA_FLAG H_MEDSTA NEW_BENE ESRD DISABLED OREC_AGE_ONLY MCAID_ANY MCAID_FULL MCAID_PART MC_ENT_ST MCR_ENT_YR MCR_ENT_MM SHSV_ACO1 NXGN_ACO CPCPL_PCMH PTD_MM 
						PD_FLAG PARTD_LIS HASPTD HASPTB HASHMO MA_INS_NM PTC_STR_RT MA_SNP MA_SNP_CD MA_SNP_DE MA_SNP_FIDE MA_SNP_IN H_DOB DOB AGE_0101 DIED DOD_YRMNDY AGE64 AGE74 AGE84 AGE85 USRD MBRZP MBRCTY MBRST SSA_SCTY RUCA1 ruca_metro1 ruca_micro1
						ruca_rural1 ADI_SCR NEWENGLAND MIDWEST WESTNORTHCENTRAL SOUTHATLANTIC EASTSCENTRAL WESTSCENTRAL MOUNTAIN PACIFIC MALE FEMALE WHITE BLACK HISPANIC ASIAN NTVAM HAWPAC MIXED OTHER 
						OTHR_LANG ENG_SPK_PRB EDUNKN EDNOHS EDHSGD EDSMCL EDCLGD INCOME FPL_LTE100 FPL_101_200 FPL_GT200 MARRIED WIDOW DIV_SEP NEV_MARRY VET_STS WORKING_SR MCBS_INST MDS3_INST NE MW SO WE);
merge sample_&YEAR. demo_&YEAR. inst_&YEAR.;
by baseid;
if CL_YR = &YEAR.;
if MCBS_INST=. then MCBS_INST=0;
if MDS3_INST=. then MDS3_INST=0;
run;

proc means data = sample_&YEAR. n sum mean;
var MCBS_INST MDS3_INST;
run;

/* 3 - getting some utilization variables */
data admnutls_&YEAR. (keep = BASEID HSPC SNF_ST SNF_DY PTA_REIMB PTB_REIMB TOT_REIMB PTD_REIMB SHSV_ACO2);
set svy_&YR.s.admnutls;

If H_HOSSW  =1 then HSPC = 1; else HSPC=0;
SNF_ST = H_SNFSTY; if SNF_ST=. then SNF_ST = 0;
SNF_DY = H_SNFDAY; if SNF_DY=. then SNF_DY = 0;

PTA_REIMB = H_PTARMB; if PTA_REIMB = . then PTA_REIMB = 0;
PTB_REIMB = H_PTBRMB; if PTB_REIMB = . then PTB_REIMB = 0;
TOT_REIMB = PTA_REIMB + PTB_REIMB; if TOT_REIMB = . then TOT_REIMB = 0;
PTD_REIMB = H_PTDPMT; if PTD_REIMB = . then PTD_REIMB = 0;

if H_ACOFLG=8 then SHSV_ACO2=1; else SHSV_ACO2=0;

run;

data sample_&YEAR. (keep = BASEID CL_YR PTA_MM PTB_MM FFS_MM MA_MM MA_FLAG PRVSUP PRVSUP_MED PRVSUP_RX VA_FLAG H_MEDSTA NEW_BENE ESRD DISABLED OREC_AGE_ONLY MCAID_ANY MCAID_FULL MCAID_PART MC_ENT_ST MCR_ENT_YR MCR_ENT_MM SHSV_ACO1 NXGN_ACO CPCPL_PCMH PTD_MM 
						PD_FLAG PARTD_LIS HASPTD HASPTB HASHMO MA_INS_NM PTC_STR_RT MA_SNP MA_SNP_CD MA_SNP_DE MA_SNP_FIDE MA_SNP_IN H_DOB DOB AGE_0101 DIED DOD_YRMNDY AGE64 AGE74 AGE84 AGE85 USRD MBRZP MBRCTY MBRST SSA_SCTY RUCA1 ruca_metro1 ruca_micro1
						ruca_rural1 ADI_SCR NEWENGLAND MIDWEST WESTNORTHCENTRAL SOUTHATLANTIC EASTSCENTRAL WESTSCENTRAL MOUNTAIN PACIFIC MALE FEMALE WHITE BLACK HISPANIC ASIAN NTVAM HAWPAC MIXED OTHER 
						OTHR_LANG ENG_SPK_PRB EDUNKN EDNOHS EDHSGD EDSMCL EDCLGD INCOME FPL_LTE100 FPL_101_200 FPL_GT200 MARRIED WIDOW DIV_SEP NEV_MARRY VET_STS WORKING_SR MCBS_INST MDS3_INST NE MW SO WE HSPC 
						SNF_ST SNF_DY PTA_REIMB PTB_REIMB TOT_REIMB PTD_REIMB SHSV_ACO);
merge sample_&YEAR. admnutls_&YEAR.;
by baseid;
if CL_YR = &YEAR.;
if &YEAR. <2017 then SHSV_ACO = SHSV_ACO2; else if &YEAR. >=2017 then SHSV_ACO = SHSV_ACO1; /* MCBS measured this differently before 2017 */
run;

/* 4 - getting weight variables */
data evrwgts_&YEAR. (keep = BASEID SV_VARSTRATUM SV_VARPSU SV_XSEE_WGT);
set svy_&YR.s.evrwgts;

SV_VARSTRATUM = SUDSTRAT; /* mcbs stratum */
SV_VARPSU = SUDUNIT;	   /* mcbs primary sampling unit */
SV_XSEE_WGT = EEYRSWGT;   /* mcbs cross-sectional ever enrolled weight -- use to get people enrolled anytime during the year - such as those who died */

run;

libname cs&YR.s "R:\Active\mcbs\&YEAR.\cost\Data\SAS Files";
data csevwgts_&YEAR. (keep = BASEID CS_VARSTRATUM CS_VARPSU CS_XSEE_WGT);
set cs&YR.s.csevwgts;

CS_VARSTRATUM = SUDSTRAT; /* mcbs stratum */
CS_VARPSU = SUDUNIT;	   /* mcbs primary sampling unit */
CS_XSEE_WGT = CSEVRWGT;   /* mcbs cross-sectional ever enrolled weight -- use to get people enrolled anytime during the year - such as those who died */

run;

data sample_&YEAR. (keep = BASEID CL_YR PTA_MM PTB_MM FFS_MM MA_MM MA_FLAG PRVSUP PRVSUP_MED PRVSUP_RX VA_FLAG H_MEDSTA NEW_BENE ESRD DISABLED OREC_AGE_ONLY MCAID_ANY MCAID_FULL MCAID_PART MC_ENT_ST MCR_ENT_YR MCR_ENT_MM SHSV_ACO1 NXGN_ACO CPCPL_PCMH PTD_MM 
						PD_FLAG PARTD_LIS HASPTD HASPTB HASHMO MA_INS_NM PTC_STR_RT MA_SNP MA_SNP_CD MA_SNP_DE MA_SNP_FIDE MA_SNP_IN H_DOB DOB AGE_0101 DIED DOD_YRMNDY AGE64 AGE74 AGE84 AGE85 USRD MBRZP MBRCTY MBRST SSA_SCTY RUCA1 ruca_metro1 ruca_micro1
						ruca_rural1 ADI_SCR NEWENGLAND MIDWEST WESTNORTHCENTRAL SOUTHATLANTIC EASTSCENTRAL WESTSCENTRAL MOUNTAIN PACIFIC MALE FEMALE WHITE BLACK HISPANIC ASIAN NTVAM HAWPAC MIXED OTHER 
						OTHR_LANG ENG_SPK_PRB EDUNKN EDNOHS EDHSGD EDSMCL EDCLGD INCOME FPL_LTE100 FPL_101_200 FPL_GT200 MARRIED WIDOW DIV_SEP NEV_MARRY VET_STS WORKING_SR MCBS_INST MDS3_INST NE MW SO WE HSPC 
						SNF_ST SNF_DY PTA_REIMB PTB_REIMB TOT_REIMB PTD_REIMB SHSV_ACO SV_VARSTRATUM SV_VARPSU SV_XSEE_WGT CS_VARSTRATUM CS_VARPSU CS_XSEE_WGT);
merge sample_&YEAR. evrwgts_&YEAR. csevwgts_&YEAR.;
by baseid;
if CL_YR = &YEAR.;
run;

/* 4a - getting information on Chrncond , Nagidis [disability] , Genhlth , Nicoalco, Prevcare -- for community-dwelling residents */

data Chrncond_&YEAR. (keep = BASEID ALZDMT_SR DPRSSN_SR DM_RP DM_T1 DM_T2 DM_PRE HF_RP IH_RP ANGINA_RP CD_RP HYST_RP CLN_CNCR_RP IDD_RP PSYCH_RP);
set svy_&YR.s.Chrncond;

ALZDMT_SR=0;
if OCALZMER=1 or OCDEMENT=1 or EMALZMER=1 or EMDEMENT=1 then ALZDMT_SR=1;
if OCALZMER not in (1,2) and OCDEMENT not in (1,2) and EMALZMER not in (1,2) and EMDEMENT not in (1,2) then ALZDMT_SR=.;  

if OCDEPRSS=1 then DPRSSN_SR=1; else DPRSSN_SR=0;
if OCBETES = 1 or OCDVISIT = 1 then DM_RP = 1; else DM_RP = 0;
if D_OCDTYP = 1 then DM_T1 = 1; else DM_T1=0;
if D_OCDTYP = 2 then DM_T2 = 1; else DM_T2=0;
if D_OCDTYP = 3 then DM_PRE = 1; else DM_PRE=0;
if OCCFAIL = 1 then HF_RP = 1; else HF_RP = 0;
if OCCHD = 1 or OCMYOCAR = 1 then IH_RP = 1; else IH_RP = 0;
if OCCHD = 1 then ANGINA_RP = 1; else ANGINA_RP = 0;
if OCEMPHYS = 1 then CD_RP=1; else CD_RP = 0;
if HYSTEREC = 1 then HYST_RP=1; else HYST_RP=0;
if OCCCOLON = 1 then CLN_CNCR_RP=1; else CLN_CNCR_RP=0;

if OCMENTAL=1 or EMMENTAL=1 then IDD_RP=1; else IDD_RP=0;

PSYCH_RP=0;
if OCDEPRSS=1 or OCPSYCHO=1 or EMDEPRSS=1 or EMPSYCHO=1 then PSYCH_RP=1;  
if OCDEPRSS not in (1,2) and OCPSYCHO not in (1,2) and EMDEPRSS not in (1,2) and EMPSYCHO not in (1,2) then PSYCH_RP=.; /* if every response is missing then code as missing */

run;

data Nagidis_&YEAR. (keep = BASEID ADL IADL COGIMP_SR DF_LIFT_RP DF_KNEL_RP DF_WALK_RP DF_DRES_RP DF_EAT_RP);
set svy_&YR.s.Nagidis;

/* ADLs */
bath=0; if HPPDBATH=1 then bath=1; else if HPPDBATH=3 and DONTBATH=1 then bath=1; 
dres=0; if HPPDDRES=1 then dres=1; else if HPPDDRES=3 and DONTDRES=1 then dres=1;
eat=0; if HPPDEAT=1 then eat=1; else if HPPDEAT=3 and DONTEAT=1 then eat=1;
char=0; if HPPDCHAR=1 then char=1; else if HPPDCHAR=3 and DONTCHAR=1 then char=1;
walk=0; if HPPDWALK=1 then walk=1; else if HPPDWALK=3 and DONTWALK=1 then walk=1;
toil=0; if HPPDTOIL=1 then toil=1; else if HPPDTOIL=3 and DONTTOIL=1 then toil=1;
ADL=bath+dres+eat+char+walk+toil; 
if HPPDBATH not in (1,2,3) and HPPDDRES not in (1,2,3) and HPPDEAT not in (1,2,3) and HPPDCHAR not in (1,2,3) and HPPDWALK not in (1,2,3) and HPPDTOIL not in (1,2,3) then ADL=.; /* if every response is missing then code as missing */
/* IADLs */
tele=0; if PRBTELE=1 then tele=1; else if PRBTELE=3 and DONTTELE=1 then tele=1;
hwrk=0; if PRBLHWK=1 then hwrk=1; else if PRBLHWK=3 and DONTLHWK=1 then hwrk=1;
hhwrk= 0; if PRBHHWK=1 then hhwrk= 1; else if PRBHHWK=3 and DONTHHWK=1 then hhwrk=1;
meal= 0; if PRBMEAL=1 then meal=1; else if PRBMEAL=3 and DONTMEAL=1 then meal=1;
shop=0; if PRBSHOP=1 then shop = 1; else if PRBSHOP=3 and DONTSHOP=1 then shop=1; 
money=0; if PRBBILS=1 then money=1; else if PRBBILS=3 and DONTBILS=1 then money=1;
IADL=tele+hwrk+hhwrk+meal+shop+money;
if PRBTELE not in (1,2,3) and PRBLHWK not in (1,2,3) and PRBHHWK not in (1,2,3) and PRBMEAL not in (1,2,3) and PRBSHOP not in (1,2,3) and PRBBILS not in (1,2,3) then IADL=.; /* if every response is missing then code as missing */

if DISDECSN=1 then COGIMP_SR=1; else COGIMP_SR=0;
if DIFLIFT in (2,3,4,5) then DF_LIFT_RP=1; else DF_LIFT_RP=0;
if DIFSTOOP in (2,3,4,5) then DF_KNEL_RP=1; else DF_KNEL_RP=0;
if DIFWALK in (2,3,4,5) then DF_WALK_RP=1; else DF_WALK_RP=0;
DF_DRES_RP=0; if HPPDDRES=1 then DF_DRES_RP=1; else if HPPDDRES=3 and DONTDRES=1 then DF_DRES_RP=1;
DF_EAT_RP=0; if HPPDEAT=1 then DF_EAT_RP=1; else if HPPDEAT=3 and DONTEAT=1 then DF_EAT_RP=1;

run;

data Genhlth_&YEAR. (keep = BASEID OVRWT OBESE HT_CM WT_KG BAD_SR_HLTH WRS_SR_HLTH);
set svy_&YR.s.Genhlth;

if BMI_CAT=3 then OVRWT=1; else OVRWT=0;
if BMI_CAT in (4,5) then OBESE=1; else OBESE=0;

HT_CM = (HEIGHTFT*30.48) + (HEIGHTIN*2.54);
WT_KG = WEIGHT*0.45359237;

if GENHELTH in (4,5) then BAD_SR_HLTH=1; else BAD_SR_HLTH=0;
if COMPHLTH in (4,5) then WRS_SR_HLTH=1; else WRS_SR_HLTH=0;

run;

data Nicoalco_&YEAR. (keep = BASEID TBC_NOW PREV_SMKR ALC_PRB);
set svy_&YR.s.Nicoalco;

if CIGNOW in (1,2) /*or SMKLSNOW in (1,2) or CIGARNOW in (1,2) or PIPENOW in (1,2) or ECIGNOW in (1,2)*/ then TBC_NOW=1; else TBC_NOW=0;
if CIG100=1 AND TBC_NOW=0 then PREV_SMKR=1; else PREV_SMKR=0;
if ALPERDAY=1 then ALC_PRB=1; else ALC_PRB=0; /* Ever 5/4 drinks or more almost every day */
run;
/* Note that the wording changed for colon cancer screening in 2019 and I need to fix it */
data Prevcare_&YEAR. (keep = BASEID BLDCHL_RP BLDPRS_RP FLU_RP PNEUM_RP MAMM_RP PAPS_RP PSA_RP COL_FOBT_RP COL_FOBT_RP_LT5 COL_SCOP_RP COL_SCOP_RP_LT5);
set svy_&YR.s.Prevcare;
if BCTAKEN in (1,2) then BLDCHL_RP=1; else if BCTAKEN not in (1,2,.) then BLDCHL_RP=0;
if BPTAKEN in (1,2) then BLDPRS_RP=1; else if BPTAKEN not in (1,2,.) then BLDPRS_RP=0;
if FLUSHOT=1 then FLU_RP=1; else if FLUSHOT not in (1,.) then FLU_RP=0;
if PNEUSHOT=1 then PNEUM_RP=1; else if PNEUSHOT not in (1,.) then PNEUM_RP=0;
if MAMMOGRM=1 then MAMM_RP=1; else if MAMMOGRM not in (1,.) then MAMM_RP=0;
if PAPSMEAR=1 then PAPS_RP=1; else if PAPSMEAR not in (1,.) then PAPS_RP=0;
if BLOODTST=1 then PSA_RP=1; else if BLOODTST not in (1,.) then PSA_RP=0;
if COLHTEST=1 or COLFDOC=1 then COL_FOBT_RP=1; else if COLHTEST not in (1,.) and COLFDOC ne 1 then COL_FOBT_RP=0;
if COL_FOBT_RP=1 and COLRECNT in (1,2,3,4) then COL_FOBT_RP_LT5=1; else if COL_FOBT_RP=0 then COL_FOBT_RP_LT5=0; else if COL_FOBT_RP=1 and COLRECNT not in (1,2,3,4) then COL_FOBT_RP_LT5=0;
if COLSCOPY=1 then COL_SCOP_RP=1; else if COLSCOPY not in (1,.) then COL_SCOP_RP=0;
if COL_SCOP_RP=1 and WHENSCOP in (1,2,3,4) then COL_SCOP_RP_LT5=1; else if COL_SCOP_RP=0 then COL_SCOP_RP_LT5=0; else if COL_SCOP_RP=1 and WHENSCOP not in (1,2,3,4) then COL_SCOP_RP_LT5=0;
run;

data health_comm_&YEAR. (keep = BASEID ALZDMT_SR DPRSSN_SR DM_RP DM_T1 DM_T2 DM_PRE HF_RP IH_RP ANGINA_RP CD_RP HYST_RP CLN_CNCR_RP IDD_RP PSYCH_RP ADL IADL COGIMP_SR DF_LIFT_RP DF_KNEL_RP DF_WALK_RP DF_DRES_RP DF_EAT_RP OVRWT OBESE 
								HT_CM WT_KG BAD_SR_HLTH WRS_SR_HLTH TBC_NOW PREV_SMKR ALC_PRB BLDCHL_RP BLDPRS_RP FLU_RP PNEUM_RP MAMM_RP PAPS_RP PSA_RP COL_FOBT_RP COL_FOBT_RP_LT5 COL_SCOP_RP COL_SCOP_RP_LT5);
merge Chrncond_&YEAR. Nagidis_&YEAR. Genhlth_&YEAR. Nicoalco_&YEAR. Prevcare_&YEAR.;
by baseid;
run;

data remove_fac_&YEAR. (keep = BASEID REMOVE);
set svy_&YR.s.Facasmnt;
REMOVE=1;
if REMOVE=1;
run;

data health_comm_&YEAR. (drop = REMOVE);
merge health_comm_&YEAR. remove_fac_&YEAR.;
by BASEID;
if REMOVE ne 1; /* deleting those beneficiaries who are in the facility file because I am going to use their information from that */
run;

/* 4b - getting information on Chrncond , Nagidis [disability] , Genhlth , Nicoalco variables -- for facility-dwelling residents from Facasmnt */

data health_fac_&YEAR. (keep = BASEID ALZDMT_SR DPRSSN_SR DM_RP HF_RP IH_RP ANGINA_RP CD_RP HYST_RP IDD_RP PSYCH_RP ADL IADL COGIMP_SR DF_LIFT_RP DF_KNEL_RP DF_WALK_RP DF_DRES_RP DF_EAT_RP OVRWT OBESE 
							   HT_CM WT_KG BAD_SR_HLTH WRS_SR_HLTH TBC_NOW PREV_SMKR ALC_PRB FLU_RP PNEUM_RP MAMM_RP PAPS_RP PSA_RP);
set svy_&YR.s.Facasmnt;

ALZDMT_SR=0; 
if ALZHMR in (1,2) or DEMENT in (1,2) then ALZDMT_SR=1; 
if ALZHMR not in (0,1,2) and DEMENT not in (0,1,2) then ALZDMT_SR=.; /* if every response is missing then code as missing */

if DEPRESS in (1,2) then DPRSSN_SR=1; else DPRSSN_SR=0;
if DIABMRN in (1,2) then DM_RP = 1; else DM_RP = 0;
if HARTFAIL in (1,2) then HF_RP = 1; else HF_RP = 0;
if CORARTDS in (1,2) or MYOCARD=1  then IH_RP = 1; else IH_RP = 0;
if CORARTDS in (1,2) then ANGINA_RP = 1; else ANGINA_RP = 0;
if ASTHCOPD in (1,2) then CD_RP=1; else CD_RP = 0;
if D_HYST=1 then HYST_RP=1; else HYST_RP=0;

if DOWNS in (1,2) or MENTDOWN=1 or MENTAUTI=1 or MENTOTHO=1 or MENTOTHN=1 then IDD_RP=1; else IDD_RP=0;

PSYCH_RP=0;
if D_MENTAL=1 or DEPRESS in (1,2) or MANICDEP in (1,2) or PSYCOTIC in (1,2) or SCHIZOPH in (1,2) then PSYCH_RP=1; 
if D_MENTAL not in (0,1) and DEPRESS not in (0,1,2) and MANICDEP not in (0,1,2) and PSYCOTIC not in (0,1,2) and SCHIZOPH not in (0,1,2) then PSYCH_RP=.; /* if every response is missing then code as missing */

/* ADLs */
bath=0; if PFBATHNG ne 0 then bath=1; /* for facility ADLs: if member does not state they are independent then assume some level of dependence even if no response */
dres=0; if PFDRSSNG ne 0 then dres=1; 
eat=0; if PFEATING ne 0 then eat=1;
char=0; if PFTRNSFR ne 0 then char=1;
walk=0; if PFLOCOMO ne 0 then walk=1; 
toil=0; if PFTOILET ne 0 then toil=1; 
ADL=bath+dres+eat+char+walk+toil;

/* IADLs */
tele=0; if DIFUSEPH=1 then tele=1; else if DIFUSEPH=3 and REASNOPH=1 then tele=1;
hwrk=1; /* this question is not asked but I am assuming light housework is a problem because member lives in a facility */
hhwrk= 1; /* this question is not asked but I am assuming heavy housework is a problem because member lives in a facility */
meal=1; /* this question is not asked but I am assuming making meals is a problem because member lives in a facility */
shop=0; if DIFSHOP=1 then shop = 1; else if DIFSHOP=3 and REASNOSH=1 then shop=1;
money=0; if DIFMONEY=1 then money=1; else if DIFMONEY=3 and REASNOMM=1 then money=1;
IADL=tele+hwrk+hhwrk+meal+shop+money;

if MENTSUM<13 or CSMEMST=1 or CSMEMLT=1 or CSCURSEA=0 or CSLOCROM=0 or CSNAMFAC=0 or CSINNH=0 or CSDECIS in (2,3) 
then COGIMP_SR=1; else COGIMP_SR=0;
if IADLIFT in (1,2,3,4) then DF_LIFT_RP=1; else DF_LIFT_RP=0;
if IADSTOOP in (1,2,3,4) then DF_KNEL_RP=1; else DF_KNEL_RP=0;
if IADWALK in (1,2,3,4) then DF_WALK_RP=1; else DF_WALK_RP=0;
DF_DRES_RP=0; if PFDRSSNG ne 0 then DF_DRES_RP=1;
DF_EAT_RP=0; if PFEATING ne 0 then DF_EAT_RP=1;

inches_sq=HEIGHT**2; BMI=(WEIGHT/inches_sq)*703; if BMI >=25 and BMI <30 then OVRWT=1; else OVRWT=0; if BMI >=30 then OBESE=1; else OBESE=0;
HT_CM = HEIGHT*2.54;
WT_KG = WEIGHT*0.45359237;
if SPHEALTH in (3,4) then BAD_SR_HLTH=1; else BAD_SR_HLTH=0;
if GENHLTH in (3,4) then WRS_SR_HLTH=1; else WRS_SR_HLTH=0;
if NOWSMOKE=1 then TBC_NOW=1; else TBC_NOW=0;
if D_SMOKE=1 AND TBC_NOW=0 then PREV_SMKR=1; else PREV_SMKR=0;
if ALCOH=1 then ALC_PRB=1; else ALC_PRB=0;

if FLUSHOT=1 then FLU_RP=1; else if FLUSHOT not in (1,.) then FLU_RP=0;
if D_PNEU=1 then PNEUM_RP=1; else if D_PNEU not in (1,.) then PNEUM_RP=0;
if MAMMOGR=1 then MAMM_RP=1; else if MAMMOGR not in (1,.) then MAMM_RP=0;
if PAPSMEAR=1 then PAPS_RP=1; else if PAPSMEAR not in (1,.) then PAPS_RP=0;
if BLOODPSA=1 then PSA_RP=1; else if BLOODPSA not in (1,.) then PSA_RP=0;

run;

data health_fac2_&YEAR. (keep = BASEID IDD2_RP);
set svy_&YR.s.mds3;
if A1510B=1 then IDD2_RP=1; 
if IDD2_RP=1;
run;

proc sort data = health_fac2_&YEAR. nodupkey; by baseid; run;

/* diabetes quality measures */
Data diab_qual_com_&YEAR. (keep=baseid dm_ftexam dm_bldtst_self dm_slfmgt_ed dm_drvs dm_hba1c dm_cntrld);
set svy_&YR.s.diabetes;
if DIATENYR=1 then dm_ftexam=1; else dm_ftexam=0;
if DIATEST=1 then dm_bldtst_self=1; else dm_bldtst_self=0;
if DIAMNGE=1 then dm_slfmgt_ed=1; else dm_slfmgt_ed=0;
dm_drvs=DIADRSAW; if DIADRSAW <0 then dm_drvs=0;
dm_hba1c=DIAHEMOC; if DIAHEMOC <0 then dm_hba1c=0;
dm_cntrld = DIACTRLD; if DIACTRLD <0 then dm_cntrld=.;

run;

/* Now seeing who has missing data for alzdmt_sr that I can get from the MDS3.0 */
Data missing_adrd_&YEAR. (keep = baseid ALZDMT_SR MDS3_INST);
merge sample_&YEAR. health_comm_&YEAR. health_fac_&YEAR. mds3_inst_&YEAR.;
by baseid;
if CL_YR = &YEAR.;
if ALZDMT_SR=.; /* missing adrd */
if MDS3_INST=1; /* in mds3.0 */
run;

proc sort data = svy_&YR.s.mds3; by baseid descending TRGT_DT; run; /* sorting by most recent assessment date */

Data missing_adrd_&YEAR. (keep = baseid ALZDMT_SR MDS3_INST TRGT_DT SUB_DT MDS_ALZDMT_SR);
merge missing_adrd_&YEAR. svy_&YR.s.mds3;
by baseid;
if MDS3_INST=1;
if I4800 in (0,1) or I4200 in (0,1); /* they have data on at least one measure */
if I4800=1 or I4200=1 then MDS_ALZDMT_SR=1; else MDS_ALZDMT_SR=0; /* they have at least one of Alz or Dementia */
run;

Data adrd_mds3_&YEAR. (keep = baseid MDS_ALZDMT_SR);
set missing_adrd_&YEAR.;
by baseid; 
if first.baseid; /* taking the most recent assessment record for those with multiple records */
run;

/* Now seeing who has missing data for psych_rp that I can get from the MDS3.0 */
Data missing_psych_&YEAR. (keep = baseid PSYCH_RP MDS3_INST);
merge sample_&YEAR. health_comm_&YEAR. health_fac_&YEAR. mds3_inst_&YEAR.;
by baseid;
if CL_YR = &YEAR.;
if PSYCH_RP=.; /* missing psych_rp */
if MDS3_INST=1; /* in mds3.0 */
run;

proc sort data = svy_&YR.s.mds3; by baseid descending TRGT_DT; run; /* sorting by most recent assessment date */

Data missing_psych_&YEAR. (keep = baseid PSYCH_RP MDS_PSYCH_RP MDS3_INST TRGT_DT SUB_DT A1510A I5700 I5800 I5900 I5950 I6000 I6100);
merge missing_psych_&YEAR. svy_&YR.s.mds3;
by baseid;
if MDS3_INST=1;
if A1510A in (0,1) or I5700 in (0,1) or I5800 in (0,1) or I5900 in (0,1) or I5950 in (0,1) or I6000 in (0,1) or I6100 in (0,1); /* they have data on at least one measure */
if A1510A=1 or I5700=1 or I5800=1 or I5900=1 or I5950=1 or I6000=1 or I6100=1 then MDS_PSYCH_RP=1; else MDS_PSYCH_RP=0; /* they have at least one of the mental health conditions (depression, Anxiety, Schizo/psycho, PTSD */
run;

Data psych_mds3_&YEAR. (keep = baseid MDS_PSYCH_RP);
set missing_psych_&YEAR.;
by baseid; 
if first.baseid; /* taking the most recent assessment record for those with multiple records */
run;

/* Now seeing who has missing data for ADLs that I can get from the MDS3.0 */
Data missing_func_&YEAR. (keep = baseid ADL MDS3_INST);
merge sample_&YEAR. health_comm_&YEAR. health_fac_&YEAR. mds3_inst_&YEAR.;
by baseid;
if CL_YR = &YEAR.;
if ADL=.; /* missing ADLs  */
if MDS3_INST=1; /* in mds3.0 */
run;

proc sort data = svy_&YR.s.mds3; by baseid descending TRGT_DT; run; /* sorting by most recent assessment date */

Data missing_func_&YEAR. (keep = baseid ADL MDS3_INST TRGT_DT SUB_DT G0120A G0110G1 G0110H1 G0110B1 G0110E1 G0110I1 MDS_ADL);
merge missing_func_&YEAR. svy_&YR.s.mds3;
by baseid;
if MDS3_INST=1;
if G0120A in (0,1,2,3,4,7,8) or G0110G1 in (0,1,2,3,4,7,8) or G0110H1 in (0,1,2,3,4,7,8) or G0110B1 in (0,1,2,3,4,7,8) or G0110E1 in (0,1,2,3,4,7,8) or G0110I1 in (0,1,2,3,4,7,8); /* they have data on at least one measure */
/* ADLs */
bath=0; if G0120A ne 0 then bath=1; /* for facility ADLs: if member does not state they are independent then assume some level of dependence */
dres=0; if G0110G1 ne 0 then dres=1; 
eat=0; if G0110H1 ne 0 then eat=1;
char=0; if G0110B1 ne 0 then char=1;
walk=0; if G0110E1 ne 0 then walk=1; 
toil=0; if G0110I1 ne 0 then toil=1; 
MDS_ADL=bath+dres+eat+char+walk+toil;
run;

Data func_mds3_&YEAR. (keep = baseid MDS_ADL);
set missing_func_&YEAR.;
by baseid; 
if first.baseid; /* taking the most recent assessment record for those with multiple records */
run;

/* then replace those missing records that were filled in in health_comm_&YEAR. and health_fac_&YEAR. from MDS 3.0 below when I merge back with the main sample */
Data sample_&YEAR. (keep = BASEID CL_YR PTA_MM PTB_MM FFS_MM MA_MM MA_FLAG PRVSUP PRVSUP_MED PRVSUP_RX VA_FLAG H_MEDSTA NEW_BENE ESRD DISABLED OREC_AGE_ONLY MCAID_ANY MCAID_FULL MCAID_PART MC_ENT_ST MCR_ENT_YR MCR_ENT_MM SHSV_ACO1 NXGN_ACO CPCPL_PCMH PTD_MM 
						PD_FLAG PARTD_LIS HASPTD HASPTB HASHMO MA_INS_NM PTC_STR_RT MA_SNP MA_SNP_CD MA_SNP_DE MA_SNP_FIDE MA_SNP_IN H_DOB DOB AGE_0101 DIED DOD_YRMNDY AGE64 AGE74 AGE84 AGE85 USRD MBRZP MBRCTY MBRST SSA_SCTY RUCA1 ruca_metro1 ruca_micro1
						ruca_rural1 ADI_SCR NEWENGLAND MIDWEST WESTNORTHCENTRAL SOUTHATLANTIC EASTSCENTRAL WESTSCENTRAL MOUNTAIN PACIFIC MALE FEMALE WHITE BLACK HISPANIC ASIAN NTVAM HAWPAC MIXED OTHER 
						OTHR_LANG ENG_SPK_PRB EDUNKN EDNOHS EDHSGD EDSMCL EDCLGD INCOME FPL_LTE100 FPL_101_200 FPL_GT200 MARRIED WIDOW DIV_SEP NEV_MARRY VET_STS WORKING_SR MCBS_INST MDS3_INST NE MW SO WE HSPC 
						SNF_ST SNF_DY PTA_REIMB PTB_REIMB TOT_REIMB PTD_REIMB SHSV_ACO SV_VARSTRATUM SV_VARPSU SV_XSEE_WGT CS_VARSTRATUM CS_VARPSU CS_XSEE_WGT ALZDMT_SR DPRSSN_SR
						DM_RP DM_T1 DM_T2 DM_PRE HF_RP IH_RP ANGINA_RP CD_RP HYST_RP CLN_CNCR_RP IDD_RP PSYCH_RP ADL IADL COGIMP_SR DF_LIFT_RP DF_KNEL_RP DF_WALK_RP DF_DRES_RP DF_EAT_RP OVRWT OBESE HT_CM WT_KG BAD_SR_HLTH WRS_SR_HLTH 
						TBC_NOW PREV_SMKR ALC_PRB BLDCHL_RP BLDPRS_RP FLU_RP PNEUM_RP MAMM_RP PAPS_RP PSA_RP PSA_RP COL_FOBT_RP COL_FOBT_RP_LT5 COL_SCOP_RP COL_SCOP_RP_LT5 dm_ftexam dm_bldtst_self dm_slfmgt_ed dm_drvs dm_hba1c dm_cntrld);
merge sample_&YEAR. health_comm_&YEAR. health_fac_&YEAR. health_fac2_&YEAR. diab_qual_com_&YEAR. adrd_mds3_&YEAR. psych_mds3_&YEAR. func_mds3_&YEAR.;
by BASEID;
if CL_YR = &YEAR.;

if IDD2_RP=1 then IDD_RP=1;
if IDD_RP=. then IDD_RP=0;
/* replace missing records that were filled in in health_comm_&YEAR. and health_fac_&YEAR. from MDS3.0 when I merge back with the main sample */
if ALZDMT_SR=. and MDS_ALZDMT_SR ne . then ALZDMT_SR=MDS_ALZDMT_SR;
if PSYCH_RP=. and MDS_PSYCH_RP ne . then PSYCH_RP=MDS_PSYCH_RP; 
if ADL=. and MDS_ADL ne . then ADL=MDS_ADL; 

run;

/* 5 - Getting information from Menthlth */
data Menthlth_&YEAR. (keep = BASEID PHQ8 PHQ8_NR PHQ2 PHQ2_NR GAD2 GAD2_NR PHQ_DP_DIF PRB_SLEEP);
set svy_&YR.s.Menthlth;

/* see: https://www.phqscreeners.com/sites/g/files/g10049256/f/201412/PHQ-9_English.pdf */
phq1=0; if PHQNOINT=2 then phq1=1; else if PHQNOINT=3 then phq1=2; else if PHQNOINT=4 then phq1=3; 
phq2=0; if PHQDEPRS=2 then phq2=1; else if PHQDEPRS=3 then phq2=2; else if PHQDEPRS=4 then phq2=3;
phq3=0; if PHQNOSLP=2 then phq3=1; else if PHQNOSLP=3 then phq3=2; else if PHQNOSLP=4 then phq3=3;
phq4=0; if PHQTIRED=2 then phq4=1; else if PHQTIRED=3 then phq4=2; else if PHQTIRED=4 then phq4=3;
phq5=0; if PHQEATNG=2 then phq5=1; else if PHQEATNG=3 then phq5=2; else if PHQEATNG=4 then phq5=3;
phq6=0; if PHQFAILR=2 then phq6=1; else if PHQFAILR=3 then phq6=2; else if PHQFAILR=4 then phq6=3;
phq7=0; if PHQTRCON=2 then phq7=1; else if PHQTRCON=3 then phq7=2; else if PHQTRCON=4 then phq7=3;
phq8=0; if PHQMOVE=2 then phq8=1; else if PHQMOVE=3 then phq8=2; else if PHQMOVE=4 then phq8=3;
PHQ8 = phq1 + phq2 + phq3 + phq4 + phq5 + phq6 + phq7 + phq8;
if PHQNOINT not in(1,2,3,4) or PHQDEPRS not in(1,2,3,4) or PHQNOSLP not in(1,2,3,4) or PHQTIRED not in(1,2,3,4) or PHQEATNG not in(1,2,3,4) or PHQFAILR not in(1,2,3,4) or PHQTRCON not in(1,2,3,4) or PHQMOVE not in(1,2,3,4) then PHQ8_NR=1;  
PHQ2 = phq1 + phq2;
if PHQNOINT not in(1,2,3,4) or PHQDEPRS not in(1,2,3,4) then PHQ2_NR=1;  

/* see: https://www.phqscreeners.com/images/sites/g/files/g10016261/f/201412/instructions.pdf */
gad1=0; if GADANXTY=2 then gad1=1; else if GADANXTY=3 then gad1=2; else if GADANXTY=4 then gad1=3;
gad2=0; if GADWORRY=2 then gad2=1; else if GADWORRY=3 then gad2=2; else if GADWORRY=4 then gad2=3; 
GAD2 = gad1 + gad2; 
if GADANXTY not in(1,2,3,4) or GADWORRY not in(1,2,3,4) then GAD2_NR=1;

if PHQPRDIF in (3,4) then PHQ_DP_DIF = 1; else PHQ_DP_DIF = 0;
if PHQNOSLP in (2,3,4) then PRB_SLEEP = 1; else PRB_SLEEP = 0;

run;

Data sample_&YEAR. (keep = BASEID CL_YR PTA_MM PTB_MM FFS_MM MA_MM MA_FLAG PRVSUP PRVSUP_MED PRVSUP_RX VA_FLAG H_MEDSTA NEW_BENE ESRD DISABLED OREC_AGE_ONLY MCAID_ANY MCAID_FULL MCAID_PART MC_ENT_ST MCR_ENT_YR MCR_ENT_MM SHSV_ACO1 NXGN_ACO CPCPL_PCMH PTD_MM 
						PD_FLAG PARTD_LIS HASPTD HASPTB HASHMO MA_INS_NM PTC_STR_RT MA_SNP MA_SNP_CD MA_SNP_DE MA_SNP_FIDE MA_SNP_IN H_DOB DOB AGE_0101 DIED DOD_YRMNDY AGE64 AGE74 AGE84 AGE85 USRD MBRZP MBRCTY MBRST SSA_SCTY RUCA1 ruca_metro1 ruca_micro1
						ruca_rural1 ADI_SCR NEWENGLAND MIDWEST WESTNORTHCENTRAL SOUTHATLANTIC EASTSCENTRAL WESTSCENTRAL MOUNTAIN PACIFIC MALE FEMALE WHITE BLACK HISPANIC ASIAN NTVAM HAWPAC MIXED OTHER 
						OTHR_LANG ENG_SPK_PRB EDUNKN EDNOHS EDHSGD EDSMCL EDCLGD INCOME FPL_LTE100 FPL_101_200 FPL_GT200 MARRIED WIDOW DIV_SEP NEV_MARRY VET_STS WORKING_SR MCBS_INST MDS3_INST NE MW SO WE HSPC 
						SNF_ST SNF_DY PTA_REIMB PTB_REIMB TOT_REIMB PTD_REIMB SHSV_ACO SV_VARSTRATUM SV_VARPSU SV_XSEE_WGT CS_VARSTRATUM CS_VARPSU CS_XSEE_WGT ALZDMT_SR DPRSSN_SR
						DM_RP DM_T1 DM_T2 DM_PRE HF_RP IH_RP ANGINA_RP CD_RP HYST_RP CLN_CNCR_RP IDD_RP PSYCH_RP ADL IADL COGIMP_SR DF_LIFT_RP DF_KNEL_RP DF_WALK_RP DF_DRES_RP DF_EAT_RP OVRWT OBESE HT_CM WT_KG BAD_SR_HLTH WRS_SR_HLTH 
						TBC_NOW PREV_SMKR ALC_PRB BLDCHL_RP BLDPRS_RP FLU_RP PNEUM_RP MAMM_RP PAPS_RP PSA_RP COL_FOBT_RP COL_FOBT_RP_LT5 COL_SCOP_RP COL_SCOP_RP_LT5 dm_ftexam dm_bldtst_self dm_slfmgt_ed dm_drvs dm_hba1c dm_cntrld PHQ8 PHQ8_NR PHQ2 PHQ2_NR GAD2 GAD2_NR PHQ_DP_DIF PRB_SLEEP);
merge sample_&YEAR. Menthlth_&YEAR.;
by BASEID;
if CL_YR = &YEAR.;
run;

/* 6 - Getting information from Accesscr */
data Accesscr_&YEAR. (keep = BASEID MED_BILS_COLLECT HC_CST_DELAY TRB_GT_CARE);
set svy_&YR.s.Accesscr;

if COLAGNCY=1 then MED_BILS_COLLECT=1; else MED_BILS_COLLECT=0;
if HCDELAY=1 then HC_CST_DELAY=1; else HC_CST_DELAY=0;
if HCTROUBL=1 then TRB_GT_CARE=1; else TRB_GT_CARE=0;

run;

Data sample_&YEAR. (keep = BASEID CL_YR PTA_MM PTB_MM FFS_MM MA_MM MA_FLAG PRVSUP PRVSUP_MED PRVSUP_RX VA_FLAG H_MEDSTA NEW_BENE ESRD DISABLED OREC_AGE_ONLY MCAID_ANY MCAID_FULL MCAID_PART MC_ENT_ST MCR_ENT_YR MCR_ENT_MM SHSV_ACO1 NXGN_ACO CPCPL_PCMH PTD_MM 
						PD_FLAG PARTD_LIS HASPTD HASPTB HASHMO MA_INS_NM PTC_STR_RT MA_SNP MA_SNP_CD MA_SNP_DE MA_SNP_FIDE MA_SNP_IN H_DOB DOB AGE_0101 DIED DOD_YRMNDY AGE64 AGE74 AGE84 AGE85 USRD MBRZP MBRCTY MBRST SSA_SCTY RUCA1 ruca_metro1 ruca_micro1
						ruca_rural1 ADI_SCR NEWENGLAND MIDWEST WESTNORTHCENTRAL SOUTHATLANTIC EASTSCENTRAL WESTSCENTRAL MOUNTAIN PACIFIC MALE FEMALE WHITE BLACK HISPANIC ASIAN NTVAM HAWPAC MIXED OTHER 
						OTHR_LANG ENG_SPK_PRB EDUNKN EDNOHS EDHSGD EDSMCL EDCLGD INCOME FPL_LTE100 FPL_101_200 FPL_GT200 MARRIED WIDOW DIV_SEP NEV_MARRY VET_STS WORKING_SR MCBS_INST MDS3_INST NE MW SO WE HSPC 
						SNF_ST SNF_DY PTA_REIMB PTB_REIMB TOT_REIMB PTD_REIMB SHSV_ACO SV_VARSTRATUM SV_VARPSU SV_XSEE_WGT CS_VARSTRATUM CS_VARPSU CS_XSEE_WGT ALZDMT_SR DPRSSN_SR
						DM_RP DM_T1 DM_T2 DM_PRE HF_RP IH_RP ANGINA_RP CD_RP HYST_RP CLN_CNCR_RP IDD_RP PSYCH_RP ADL IADL COGIMP_SR DF_LIFT_RP DF_KNEL_RP DF_WALK_RP DF_DRES_RP DF_EAT_RP OVRWT OBESE HT_CM WT_KG BAD_SR_HLTH WRS_SR_HLTH 
						TBC_NOW PREV_SMKR ALC_PRB BLDCHL_RP BLDPRS_RP FLU_RP PNEUM_RP MAMM_RP PAPS_RP PSA_RP COL_FOBT_RP COL_FOBT_RP_LT5 COL_SCOP_RP COL_SCOP_RP_LT5 dm_ftexam dm_bldtst_self dm_slfmgt_ed dm_drvs dm_hba1c dm_cntrld PHQ8 PHQ8_NR PHQ2 PHQ2_NR GAD2 GAD2_NR PHQ_DP_DIF PRB_SLEEP MED_BILS_COLLECT HC_CST_DELAY 
						TRB_GT_CARE);
merge sample_&YEAR. Accesscr_&YEAR.;
by BASEID;
if CL_YR = &YEAR.;
run;

/* 7 - Getting information from Satwcare */
data Satwcare_&YEAR. (keep = BASEID GPS_SCR NOMD_CST MD_CARE_HLTH MD_TRVL_EASE SPC_ACCS_SAT QUAL_SAT INFO_SAT);
set svy_&YR.s.Satwcare;

/* Calculating General Patient Satisfaction Score -- derived from: https://www.cms.gov/Research-Statistics-Data-and-Systems/Research/MCBS/Downloads/Quality_Patient_Experience.pdf */
Q1=.; if MCQUALTY=4 then Q1=0; else if MCQUALTY=3 then Q1=1; else if MCQUALTY=2 then Q1=2; else if MCQUALTY=1 then Q1=3;
Q2=.; if MCAVAIL=4 then Q2=0; else if MCAVAIL=3 then Q2=1; else if MCAVAIL=2 then Q2=2; else if MCAVAIL=1 then Q2=3;
Q3=.; if MCEASE=4 then Q3=0; else if MCEASE=3 then Q3=1; else if MCEASE=2 then Q3=2; else if MCEASE=1 then Q3=3;
Q4=.; if MCCOSTS=4 then Q4=0; else if MCCOSTS=3 then Q4=1; else if MCCOSTS=2 then Q4=2; else if MCCOSTS=1 then Q4=3;
Q5=.; if MCINFO=4 then Q5=0; else if MCINFO=3 then Q5=1; else if MCINFO=2 then Q5=2; else if MCINFO=1 then Q5=3;
Q6=.; if MCFOLUP=4 then Q6=0; else if MCFOLUP=3 then Q6=1; else if MCFOLUP=2 then Q6=2; else if MCFOLUP=1 then Q6=3;
Q7=.; if MCCONCRN=4 then Q7=0; else if MCCONCRN=3 then Q7=1; else if MCCONCRN=2 then Q7=2; else if MCCONCRN=1 then Q7=3;
Q8=.; if MCSAMLOC=4 then Q8=0; else if MCSAMLOC=3 then Q8=1; else if MCSAMLOC=2 then Q8=2; else if MCSAMLOC=1 then Q8=3;
Q9=.; if MCSPECAR=4 then Q9=0; else if MCSPECAR=3 then Q9=1; else if MCSPECAR=2 then Q9=2; else if MCSPECAR=1 then Q9=3;
Q10=.; if MCTELANS=4 then Q10=0; else if MCTELANS=3 then Q10=1; else if MCTELANS=2 then Q10=2; else if MCTELANS=1 then Q10=3;
GPS_SCR = Q1 + Q2 + Q3 + Q4 + Q5 + Q6 + Q7 + Q8 + Q9 + Q10;
if Q1=. or Q2=. or Q3=. or Q4=. or Q5=. or Q6=. or Q7=. or Q8=. or Q9=. or Q10=. then GPS_SCR=.; /* setting GPS score to missing if missing any questions */

if SCRCOST=1 then NOMD_CST=1; else NOMD_CST=0;
if MCCONCRN in (1,2) then MD_CARE_HLTH=1; else MD_CARE_HLTH=0;
if MCEASE in (1,2) then MD_TRVL_EASE=1; else MD_TRVL_EASE=0;
if MCSPECAR in (1,2) then SPC_ACCS_SAT=1; else SPC_ACCS_SAT=0;
if MCQUALTY in (1,2) then QUAL_SAT=1; else QUAL_SAT=0;
if MCINFO in (1,2) then INFO_SAT=1; else INFO_SAT=0;

run;

Data sample_&YEAR. (keep = BASEID CL_YR PTA_MM PTB_MM FFS_MM MA_MM MA_FLAG PRVSUP PRVSUP_MED PRVSUP_RX VA_FLAG H_MEDSTA NEW_BENE ESRD DISABLED OREC_AGE_ONLY MCAID_ANY MCAID_FULL MCAID_PART MC_ENT_ST MCR_ENT_YR MCR_ENT_MM SHSV_ACO1 NXGN_ACO CPCPL_PCMH PTD_MM 
						PD_FLAG PARTD_LIS HASPTD HASPTB HASHMO MA_INS_NM PTC_STR_RT MA_SNP MA_SNP_CD MA_SNP_DE MA_SNP_FIDE MA_SNP_IN H_DOB DOB AGE_0101 DIED DOD_YRMNDY AGE64 AGE74 AGE84 AGE85 USRD MBRZP MBRCTY MBRST SSA_SCTY RUCA1 ruca_metro1 ruca_micro1
						ruca_rural1 ADI_SCR NEWENGLAND MIDWEST WESTNORTHCENTRAL SOUTHATLANTIC EASTSCENTRAL WESTSCENTRAL MOUNTAIN PACIFIC MALE FEMALE WHITE BLACK HISPANIC ASIAN NTVAM HAWPAC MIXED OTHER 
						OTHR_LANG ENG_SPK_PRB EDUNKN EDNOHS EDHSGD EDSMCL EDCLGD INCOME FPL_LTE100 FPL_101_200 FPL_GT200 MARRIED WIDOW DIV_SEP NEV_MARRY VET_STS WORKING_SR MCBS_INST MDS3_INST NE MW SO WE HSPC 
						SNF_ST SNF_DY PTA_REIMB PTB_REIMB TOT_REIMB PTD_REIMB SHSV_ACO SV_VARSTRATUM SV_VARPSU SV_XSEE_WGT CS_VARSTRATUM CS_VARPSU CS_XSEE_WGT ALZDMT_SR DPRSSN_SR
						DM_RP DM_T1 DM_T2 DM_PRE HF_RP IH_RP ANGINA_RP CD_RP HYST_RP CLN_CNCR_RP IDD_RP PSYCH_RP ADL IADL COGIMP_SR DF_LIFT_RP DF_KNEL_RP DF_WALK_RP DF_DRES_RP DF_EAT_RP OVRWT OBESE HT_CM WT_KG BAD_SR_HLTH WRS_SR_HLTH 
						TBC_NOW PREV_SMKR ALC_PRB BLDCHL_RP BLDPRS_RP FLU_RP PNEUM_RP MAMM_RP PAPS_RP PSA_RP COL_FOBT_RP COL_FOBT_RP_LT5 COL_SCOP_RP COL_SCOP_RP_LT5 dm_ftexam dm_bldtst_self dm_slfmgt_ed dm_drvs dm_hba1c dm_cntrld PHQ8 PHQ8_NR PHQ2 PHQ2_NR GAD2 GAD2_NR PHQ_DP_DIF PRB_SLEEP MED_BILS_COLLECT HC_CST_DELAY 
						TRB_GT_CARE GPS_SCR NOMD_CST MD_CARE_HLTH MD_TRVL_EASE SPC_ACCS_SAT QUAL_SAT INFO_SAT);
merge sample_&YEAR. Satwcare_&YEAR.;
by BASEID;
if CL_YR = &YEAR.;
run;

/* 8 - Getting information from Pmuse -- removed in 2017
data pmuse_&YEAR. (keep = BASEID NORX_CST);
set svy_&YR.s.pmuse;

if SCPMCOST=1 then NORX_CST=1; else NORX_CST=0;

run;

Data sample_&YEAR. (keep = BASEID CL_YR PTA_MM PTB_MM FFS_MM MA_FLAG PD_FLAG PRVSUP PRVSUP_MED PRVSUP_RX VA_FLAG H_MEDSTA NEW_BENE MCD_IND ESRD DSBLD OREC_AGE_ONLY   H_DOB DOB AGE_0101 DIED DOD_YRMNDY 
						   USRD MBRZP MBRCTY MBRST SSA_SCTY RUCA1 ruca_metro1 ruca_micro1 ruca_rural1 ADI_SCR MALE WHIT BLCK HISP ASIN NTAM OTHR RCUNKN OTHR_LANG ENG_SPK_PRB EDUNKN EDNOHS EDHSGD EDSMCL 
						   EDCLGD INCOME FPL_LTE100 FPL_101_200 FPL_GT200 MARRIED VET_STS INST NE MW SO WE HSPC SNF_ST SNF_DY PTA_REIMB PTB_REIMB TOT_REIMB PTD_REIMB 
						   SHSV_ACO SV_VARSTRATUM SV_VARPSU SV_XSEE_WGT CS_VARSTRATUM CS_VARPSU CS_XSEE_WGT ALZDMT_SR DPRSSN_SR DM_RP DM_T1 DM_T2 DM_PRE HF_RP IH_RP ANGINA_RP CD_RP ADL IADL OVRWT OBESE TBC_NOW PREV_SMKR ALC_PRB PHQ_DP PHQ_DP_MYB PHQ_DP_DIF PRB_SLEEP 
						   MED_BILS_COLLECT HC_CST_DELAY TRB_GT_CARE NOMD_CST MD_CARE_HLTH MD_TRVL_EASE SPC_ACCS_SAT);
merge sample_&YEAR. pmuse_&YEAR.;
by BASEID;
if CL_YR = &YEAR.;
run; */

/* 9 - Getting information from foodins */
data foodins_&YEAR. (keep = BASEID AFRD_RPLC_FOOD AFRD_EAT_HLTH NOAFRD_EAT_LESS NOAFRD_HUNGRY);
set svy_&YR.s.foodins;

if FOODLAST in (1,2) then AFRD_RPLC_FOOD=1; else AFRD_RPLC_FOOD=0;
if AFFDMEAL in (1,2) then AFRD_EAT_HLTH=1; else AFRD_EAT_HLTH=0;
if EATLESS=1 then NOAFRD_EAT_LESS=1; else NOAFRD_EAT_LESS=0;
if HUNGRY=1 then NOAFRD_HUNGRY=1; else NOAFRD_HUNGRY=0;

run;

Data sample_&YEAR. (keep = BASEID CL_YR PTA_MM PTB_MM FFS_MM MA_MM MA_FLAG PRVSUP PRVSUP_MED PRVSUP_RX VA_FLAG H_MEDSTA NEW_BENE ESRD DISABLED OREC_AGE_ONLY MCAID_ANY MCAID_FULL MCAID_PART MC_ENT_ST MCR_ENT_YR MCR_ENT_MM SHSV_ACO1 NXGN_ACO CPCPL_PCMH PTD_MM 
						PD_FLAG PARTD_LIS HASPTD HASPTB HASHMO MA_INS_NM PTC_STR_RT MA_SNP MA_SNP_CD MA_SNP_DE MA_SNP_FIDE MA_SNP_IN H_DOB DOB AGE_0101 DIED DOD_YRMNDY AGE64 AGE74 AGE84 AGE85 USRD MBRZP MBRCTY MBRST SSA_SCTY RUCA1 ruca_metro1 ruca_micro1
						ruca_rural1 ADI_SCR NEWENGLAND MIDWEST WESTNORTHCENTRAL SOUTHATLANTIC EASTSCENTRAL WESTSCENTRAL MOUNTAIN PACIFIC MALE FEMALE WHITE BLACK HISPANIC ASIAN NTVAM HAWPAC MIXED OTHER 
						OTHR_LANG ENG_SPK_PRB EDUNKN EDNOHS EDHSGD EDSMCL EDCLGD INCOME FPL_LTE100 FPL_101_200 FPL_GT200 MARRIED WIDOW DIV_SEP NEV_MARRY VET_STS WORKING_SR MCBS_INST MDS3_INST NE MW SO WE HSPC 
						SNF_ST SNF_DY PTA_REIMB PTB_REIMB TOT_REIMB PTD_REIMB SHSV_ACO SV_VARSTRATUM SV_VARPSU SV_XSEE_WGT CS_VARSTRATUM CS_VARPSU CS_XSEE_WGT ALZDMT_SR DPRSSN_SR
						DM_RP DM_T1 DM_T2 DM_PRE HF_RP IH_RP ANGINA_RP CD_RP HYST_RP CLN_CNCR_RP IDD_RP PSYCH_RP ADL IADL COGIMP_SR DF_LIFT_RP DF_KNEL_RP DF_WALK_RP DF_DRES_RP DF_EAT_RP OVRWT OBESE HT_CM WT_KG BAD_SR_HLTH WRS_SR_HLTH 
						TBC_NOW PREV_SMKR ALC_PRB BLDCHL_RP BLDPRS_RP FLU_RP PNEUM_RP MAMM_RP PAPS_RP PSA_RP COL_FOBT_RP COL_FOBT_RP_LT5 COL_SCOP_RP COL_SCOP_RP_LT5 dm_ftexam dm_bldtst_self dm_slfmgt_ed dm_drvs dm_hba1c dm_cntrld PHQ8 PHQ8_NR PHQ2 PHQ2_NR GAD2 GAD2_NR PHQ_DP_DIF PRB_SLEEP MED_BILS_COLLECT HC_CST_DELAY 
						TRB_GT_CARE GPS_SCR NOMD_CST MD_CARE_HLTH MD_TRVL_EASE SPC_ACCS_SAT QUAL_SAT INFO_SAT AFRD_RPLC_FOOD AFRD_EAT_HLTH NOAFRD_EAT_LESS NOAFRD_HUNGRY);
merge sample_&YEAR. foodins_&YEAR.;
by BASEID;
if CL_YR = &YEAR.;
run;

/* 10 - Getting information from incasset */
Data incasset_&YEAR. (keep = BASEID OWN_HOME SECT_EGHT HOME_VALU);
set svy_&YR.s.incasset;

if IAWNHOME=1 then OWN_HOME=1; else OWN_HOME=0;
if SECTION8=1 then SECT_EGHT=1; else SECT_EGHT=0;
HOME_VALU=HOMEEVAL;

run;

Data sample_&YEAR. (keep = BASEID CL_YR PTA_MM PTB_MM FFS_MM MA_MM MA_FLAG PRVSUP PRVSUP_MED PRVSUP_RX VA_FLAG H_MEDSTA NEW_BENE ESRD DISABLED OREC_AGE_ONLY MCAID_ANY MCAID_FULL MCAID_PART MC_ENT_ST MCR_ENT_YR MCR_ENT_MM SHSV_ACO1 NXGN_ACO CPCPL_PCMH PTD_MM 
						PD_FLAG PARTD_LIS HASPTD HASPTB HASHMO MA_INS_NM PTC_STR_RT MA_SNP MA_SNP_CD MA_SNP_DE MA_SNP_FIDE MA_SNP_IN H_DOB DOB AGE_0101 DIED DOD_YRMNDY AGE64 AGE74 AGE84 AGE85 USRD MBRZP MBRCTY MBRST SSA_SCTY RUCA1 ruca_metro1 ruca_micro1
						ruca_rural1 ADI_SCR NEWENGLAND MIDWEST WESTNORTHCENTRAL SOUTHATLANTIC EASTSCENTRAL WESTSCENTRAL MOUNTAIN PACIFIC MALE FEMALE WHITE BLACK HISPANIC ASIAN NTVAM HAWPAC MIXED OTHER 
						OTHR_LANG ENG_SPK_PRB EDUNKN EDNOHS EDHSGD EDSMCL EDCLGD INCOME FPL_LTE100 FPL_101_200 FPL_GT200 MARRIED WIDOW DIV_SEP NEV_MARRY VET_STS WORKING_SR MCBS_INST MDS3_INST NE MW SO WE HSPC 
						SNF_ST SNF_DY PTA_REIMB PTB_REIMB TOT_REIMB PTD_REIMB SHSV_ACO SV_VARSTRATUM SV_VARPSU SV_XSEE_WGT CS_VARSTRATUM CS_VARPSU CS_XSEE_WGT ALZDMT_SR DPRSSN_SR
						DM_RP DM_T1 DM_T2 DM_PRE HF_RP IH_RP ANGINA_RP CD_RP HYST_RP CLN_CNCR_RP IDD_RP PSYCH_RP ADL IADL COGIMP_SR DF_LIFT_RP DF_KNEL_RP DF_WALK_RP DF_DRES_RP DF_EAT_RP OVRWT OBESE HT_CM WT_KG BAD_SR_HLTH WRS_SR_HLTH 
						TBC_NOW PREV_SMKR ALC_PRB BLDCHL_RP BLDPRS_RP FLU_RP PNEUM_RP MAMM_RP PAPS_RP PSA_RP COL_FOBT_RP COL_FOBT_RP_LT5 COL_SCOP_RP COL_SCOP_RP_LT5 dm_ftexam dm_bldtst_self dm_slfmgt_ed dm_drvs dm_hba1c dm_cntrld PHQ8 PHQ8_NR PHQ2 PHQ2_NR GAD2 GAD2_NR PHQ_DP_DIF PRB_SLEEP MED_BILS_COLLECT HC_CST_DELAY 
						TRB_GT_CARE GPS_SCR NOMD_CST MD_CARE_HLTH MD_TRVL_EASE SPC_ACCS_SAT QUAL_SAT INFO_SAT AFRD_RPLC_FOOD AFRD_EAT_HLTH NOAFRD_EAT_LESS NOAFRD_HUNGRY OWN_HOME SECT_EGHT HOME_VALU);
merge sample_&YEAR. incasset_&YEAR. ;
by BASEID;
if CL_YR = &YEAR.;
run;

/* 11 - Getting information from hhchar*/
data hhchar_&YEAR. (keep = BASEID LIVE_ALON N_HHLD);
set svy_&YR.s.hhchar;

if D_HHCOMP=1 or D_COMPHH=1 then LIVE_ALON=1; else LIVE_ALON=0;
N_HHLD=D_HHTOT;

run;

Data sample_&YEAR. (keep = BASEID CL_YR PTA_MM PTB_MM FFS_MM MA_MM MA_FLAG PRVSUP PRVSUP_MED PRVSUP_RX VA_FLAG H_MEDSTA NEW_BENE ESRD DISABLED OREC_AGE_ONLY MCAID_ANY MCAID_FULL MCAID_PART MC_ENT_ST MCR_ENT_YR MCR_ENT_MM SHSV_ACO1 NXGN_ACO CPCPL_PCMH PTD_MM 
						PD_FLAG PARTD_LIS HASPTD HASPTB HASHMO MA_INS_NM PTC_STR_RT MA_SNP MA_SNP_CD MA_SNP_DE MA_SNP_FIDE MA_SNP_IN H_DOB DOB AGE_0101 DIED DOD_YRMNDY AGE64 AGE74 AGE84 AGE85 USRD MBRZP MBRCTY MBRST SSA_SCTY RUCA1 ruca_metro1 ruca_micro1
						ruca_rural1 ADI_SCR NEWENGLAND MIDWEST WESTNORTHCENTRAL SOUTHATLANTIC EASTSCENTRAL WESTSCENTRAL MOUNTAIN PACIFIC MALE FEMALE WHITE BLACK HISPANIC ASIAN NTVAM HAWPAC MIXED OTHER 
						OTHR_LANG ENG_SPK_PRB EDUNKN EDNOHS EDHSGD EDSMCL EDCLGD INCOME FPL_LTE100 FPL_101_200 FPL_GT200 MARRIED WIDOW DIV_SEP NEV_MARRY VET_STS WORKING_SR MCBS_INST MDS3_INST NE MW SO WE HSPC 
						SNF_ST SNF_DY PTA_REIMB PTB_REIMB TOT_REIMB PTD_REIMB SHSV_ACO SV_VARSTRATUM SV_VARPSU SV_XSEE_WGT CS_VARSTRATUM CS_VARPSU CS_XSEE_WGT ALZDMT_SR DPRSSN_SR
						DM_RP DM_T1 DM_T2 DM_PRE HF_RP IH_RP ANGINA_RP CD_RP HYST_RP CLN_CNCR_RP IDD_RP PSYCH_RP ADL IADL COGIMP_SR DF_LIFT_RP DF_KNEL_RP DF_WALK_RP DF_DRES_RP DF_EAT_RP OVRWT OBESE HT_CM WT_KG BAD_SR_HLTH WRS_SR_HLTH 
						TBC_NOW PREV_SMKR ALC_PRB BLDCHL_RP BLDPRS_RP FLU_RP PNEUM_RP MAMM_RP PAPS_RP PSA_RP COL_FOBT_RP COL_FOBT_RP_LT5 COL_SCOP_RP COL_SCOP_RP_LT5 dm_ftexam dm_bldtst_self dm_slfmgt_ed dm_drvs dm_hba1c dm_cntrld PHQ8 PHQ8_NR PHQ2 PHQ2_NR GAD2 GAD2_NR PHQ_DP_DIF PRB_SLEEP MED_BILS_COLLECT HC_CST_DELAY 
						TRB_GT_CARE GPS_SCR NOMD_CST MD_CARE_HLTH MD_TRVL_EASE SPC_ACCS_SAT QUAL_SAT INFO_SAT AFRD_RPLC_FOOD AFRD_EAT_HLTH NOAFRD_EAT_LESS NOAFRD_HUNGRY OWN_HOME SECT_EGHT HOME_VALU 
						LIVE_ALON N_HHLD);
merge sample_&YEAR. hhchar_&YEAR. ;
by BASEID;
if CL_YR = &YEAR.;
run;

/* 12 - Getting information from uscare */
data uscare_&YEAR. (keep = BASEID CS_USC_XSEE_WGT SV_USC_XSEE_WGT TUP_SCR USOC USOC_PCP USOC_SPC SPC_SR PCP_SPC_COORD
					USOC_VS TEST_SR TEST_FLWP_SR NEED_HOME_SR HELP_HOME_SR RX_SR INFO_RX_SR);
set svy_&YR.s.uscare;

if USEWT ne . then CS_USC_XSEE_WGT=USEWT; else if CS_USC_XSEE_WGT=. then CS_USC_XSEE_WGT=USSEWT;
if USCWT ne . then SV_USC_XSEE_WGT=USCWT; else if SV_USC_XSEE_WGT=. then SV_USC_XSEE_WGT=USSCWT;

/* Calculating Trust in Usual Physician Score -- derived from: https://www.cms.gov/Research-Statistics-Data-and-Systems/Research/MCBS/Downloads/Quality_Patient_Experience.pdf */
Q1=.; if USCKEVRY=4 then Q1=0; else if USCKEVRY=3 then Q1=1; else if USCKEVRY=2 then Q1=2; else if USCKEVRY=1 then Q1=3;
Q2=.; if USCOMPET=4 then Q2=0; else if USCOMPET=3 then Q2=1; else if USCOMPET=2 then Q2=2; else if USCOMPET=1 then Q2=3;
Q3=.; if USUNHIST=4 then Q3=0; else if USUNHIST=3 then Q3=1; else if USUNHIST=2 then Q3=2; else if USUNHIST=1 then Q3=3;
Q4=.; if USUNWRNG=4 then Q4=0; else if USUNWRNG=3 then Q4=1; else if USUNWRNG=2 then Q4=2; else if USUNWRNG=1 then Q4=3;
Q5=.; if USHURRY=4 then Q5=0; else if USHURRY=3 then Q5=1; else if USHURRY=2 then Q5=2; else if USHURRY=1 then Q5=3;
Q6=.; if USEXPPRB=4 then Q6=0; else if USEXPPRB=3 then Q6=1; else if USEXPPRB=2 then Q6=2; else if USEXPPRB=1 then Q6=3;
Q7=.; if USDISCUS=4 then Q7=0; else if USDISCUS=3 then Q7=1; else if USDISCUS=2 then Q7=2; else if USDISCUS=1 then Q7=3;
Q8=.; if USFAVOR=4 then Q8=0; else if USFAVOR=3 then Q8=1; else if USFAVOR=2 then Q8=2; else if USFAVOR=1 then Q8=3;
Q9=.; if USTELALL=4 then Q9=0; else if USTELALL=3 then Q9=1; else if USTELALL=2 then Q9=2; else if USTELALL=1 then Q9=3;
Q10=.; if USANSQUX=4 then Q10=0; else if USANSQUX=3 then Q10=1; else if USANSQUX=2 then Q10=2; else if USANSQUX=1 then Q10=3;
Q11=.; if USCONFID=4 then Q11=0; else if USCONFID=3 then Q11=1; else if USCONFID=2 then Q11=2; else if USCONFID=1 then Q11=3;
Q12=.; if USDEPEND=4 then Q12=0; else if USDEPEND=3 then Q12=1; else if USDEPEND=2 then Q12=2; else if USDEPEND=1 then Q12=3;
TUP_SCR = Q1 + Q2 + Q3 + Q4 + Q5 + Q6 + Q7 + Q8 + Q9 + Q10 + Q11 + Q12;
if Q1=. or Q2=. or Q3=. or Q4=. or Q5=. or Q6=. or Q7=. or Q8=. or Q9=. or Q10=. or Q11=. or Q12=. then TUP_SCR=.; /* setting TUP score to missing if missing any questions */

if (PLACEPAR=1 and PLACEKND in (1,2,3,4,6,7,8,10,13)) or USUALDOC=1 then USOC=1; else USOC=0;
if USOC=1 and ( (MDSPEC in (8,10,12,13,16,24,36)) or (PVSPEC in (8,10,12,13,16,24,36)) ) then USOC_PCP=1; else USOC_PCP=0;
if USOC=1 and ( (MDSPEC in (1,2,3,5,7,9,11,14,15,17,18,19,20,21,22,25,26,27,28,29,30,31,32,33,34,35,38,39,91))
or (PVSPEC in (1,2,3,5,7,9,11,14,15,17,18,19,20,21,22,25,26,27,28,29,30,31,32,33,34,35,38,39,91)) )
then USOC_SPC=1; else USOC_SPC=0;
if SPCLCARE=1 or CARESPCL=1 then SPC_SR=1; else SPC_SR=0;
if DRINFRMD in (3,4) then PCP_SPC_COORD=1; else PCP_SPC_COORD=0;

if USOC=1 and PROVYR=1 then USOC_VS=1; else USOC_VS=0;
if ORDRTEST=1 then TEST_SR=1; else TEST_SR=0;
if TEST_SR=1 and TSTFLWUP in (3,4) then TEST_FLWP_SR=1; else if TEST_SR=1 and TSTFLWUP not in (3,4) then TEST_FLWP_SR=0; 
if HLTHSRVC=1 then NEED_HOME_SR=1; else NEED_HOME_SR=0;
if NEED_HOME_SR=1 and SRVCHELP in (3,4) then HELP_HOME_SR=1; else if NEED_HOME_SR=1 and SRVCHELP not in (3,4) then HELP_HOME_SR=0; 
if ANYRX=1 then RX_SR=1; else RX_SR=0;
if RX_SR=1 and TALKRX in (3,4) then INFO_RX_SR=1; else if RX_SR=1 and TALKRX not in (3,4) then INFO_RX_SR=0;

run;

Data sample_&YEAR. (keep = BASEID CL_YR PTA_MM PTB_MM FFS_MM MA_MM MA_FLAG PRVSUP PRVSUP_MED PRVSUP_RX VA_FLAG H_MEDSTA NEW_BENE ESRD DISABLED OREC_AGE_ONLY MCAID_ANY MCAID_FULL MCAID_PART MC_ENT_ST MCR_ENT_YR MCR_ENT_MM SHSV_ACO1 NXGN_ACO CPCPL_PCMH PTD_MM 
						PD_FLAG PARTD_LIS HASPTD HASPTB HASHMO MA_INS_NM PTC_STR_RT MA_SNP MA_SNP_CD MA_SNP_DE MA_SNP_FIDE MA_SNP_IN H_DOB DOB AGE_0101 DIED DOD_YRMNDY AGE64 AGE74 AGE84 AGE85 USRD MBRZP MBRCTY MBRST SSA_SCTY RUCA1 ruca_metro1 ruca_micro1
						ruca_rural1 ADI_SCR NEWENGLAND MIDWEST WESTNORTHCENTRAL SOUTHATLANTIC EASTSCENTRAL WESTSCENTRAL MOUNTAIN PACIFIC MALE FEMALE WHITE BLACK HISPANIC ASIAN NTVAM HAWPAC MIXED OTHER 
						OTHR_LANG ENG_SPK_PRB EDUNKN EDNOHS EDHSGD EDSMCL EDCLGD INCOME FPL_LTE100 FPL_101_200 FPL_GT200 MARRIED WIDOW DIV_SEP NEV_MARRY VET_STS WORKING_SR MCBS_INST MDS3_INST NE MW SO WE HSPC 
						SNF_ST SNF_DY PTA_REIMB PTB_REIMB TOT_REIMB PTD_REIMB SHSV_ACO SV_VARSTRATUM SV_VARPSU SV_XSEE_WGT CS_VARSTRATUM CS_VARPSU CS_XSEE_WGT ALZDMT_SR DPRSSN_SR
						DM_RP DM_T1 DM_T2 DM_PRE HF_RP IH_RP ANGINA_RP CD_RP HYST_RP CLN_CNCR_RP IDD_RP PSYCH_RP ADL IADL COGIMP_SR DF_LIFT_RP DF_KNEL_RP DF_WALK_RP DF_DRES_RP DF_EAT_RP OVRWT OBESE HT_CM WT_KG BAD_SR_HLTH WRS_SR_HLTH 
						TBC_NOW PREV_SMKR ALC_PRB BLDCHL_RP BLDPRS_RP FLU_RP PNEUM_RP MAMM_RP PAPS_RP PSA_RP COL_FOBT_RP COL_FOBT_RP_LT5 COL_SCOP_RP COL_SCOP_RP_LT5 dm_ftexam dm_bldtst_self dm_slfmgt_ed dm_drvs dm_hba1c dm_cntrld PHQ8 PHQ8_NR PHQ2 PHQ2_NR GAD2 GAD2_NR PHQ_DP_DIF PRB_SLEEP MED_BILS_COLLECT HC_CST_DELAY 
						TRB_GT_CARE GPS_SCR NOMD_CST MD_CARE_HLTH MD_TRVL_EASE SPC_ACCS_SAT QUAL_SAT INFO_SAT AFRD_RPLC_FOOD AFRD_EAT_HLTH NOAFRD_EAT_LESS NOAFRD_HUNGRY OWN_HOME SECT_EGHT HOME_VALU 
						LIVE_ALON N_HHLD CS_USC_XSEE_WGT SV_USC_XSEE_WGT TUP_SCR USOC USOC_PCP USOC_SPC SPC_SR PCP_SPC_COORD USOC_VS TEST_SR TEST_FLWP_SR NEED_HOME_SR HELP_HOME_SR RX_SR INFO_RX_SR);
merge sample_&YEAR. uscare_&YEAR.;
by BASEID;
if CL_YR = &YEAR.;
run; 

/* 13 - Getting information from vishear */
data vishear_&YEAR. (keep = baseid HEAR_PRB HEAR_AID);
set svy_&YR.s.vishear;
if HCTROUB in (2,3,4) then HEAR_PRB=1; else HEAR_PRB=0;
if HCHELP = 1 then HEAR_AID=1; else HEAR_AID=0;
run;

Data sample_&YEAR. (keep = BASEID CL_YR PTA_MM PTB_MM FFS_MM MA_MM MA_FLAG PRVSUP PRVSUP_MED PRVSUP_RX VA_FLAG H_MEDSTA NEW_BENE ESRD DISABLED OREC_AGE_ONLY MCAID_ANY MCAID_FULL MCAID_PART MC_ENT_ST MCR_ENT_YR MCR_ENT_MM SHSV_ACO1 NXGN_ACO CPCPL_PCMH PTD_MM 
						PD_FLAG PARTD_LIS HASPTD HASPTB HASHMO MA_INS_NM PTC_STR_RT MA_SNP MA_SNP_CD MA_SNP_DE MA_SNP_FIDE MA_SNP_IN H_DOB DOB AGE_0101 DIED DOD_YRMNDY AGE64 AGE74 AGE84 AGE85 USRD MBRZP MBRCTY MBRST SSA_SCTY RUCA1 ruca_metro1 ruca_micro1
						ruca_rural1 ADI_SCR NEWENGLAND MIDWEST WESTNORTHCENTRAL SOUTHATLANTIC EASTSCENTRAL WESTSCENTRAL MOUNTAIN PACIFIC MALE FEMALE WHITE BLACK HISPANIC ASIAN NTVAM HAWPAC MIXED OTHER 
						OTHR_LANG ENG_SPK_PRB EDUNKN EDNOHS EDHSGD EDSMCL EDCLGD INCOME FPL_LTE100 FPL_101_200 FPL_GT200 MARRIED WIDOW DIV_SEP NEV_MARRY VET_STS WORKING_SR MCBS_INST MDS3_INST NE MW SO WE HSPC 
						SNF_ST SNF_DY PTA_REIMB PTB_REIMB TOT_REIMB PTD_REIMB SHSV_ACO SV_VARSTRATUM SV_VARPSU SV_XSEE_WGT CS_VARSTRATUM CS_VARPSU CS_XSEE_WGT ALZDMT_SR DPRSSN_SR
						DM_RP DM_T1 DM_T2 DM_PRE HF_RP IH_RP ANGINA_RP CD_RP HYST_RP CLN_CNCR_RP IDD_RP PSYCH_RP ADL IADL COGIMP_SR DF_LIFT_RP DF_KNEL_RP DF_WALK_RP DF_DRES_RP DF_EAT_RP OVRWT OBESE HT_CM WT_KG BAD_SR_HLTH WRS_SR_HLTH 
						TBC_NOW PREV_SMKR ALC_PRB BLDCHL_RP BLDPRS_RP FLU_RP PNEUM_RP MAMM_RP PAPS_RP PSA_RP COL_FOBT_RP COL_FOBT_RP_LT5 COL_SCOP_RP COL_SCOP_RP_LT5 dm_ftexam dm_bldtst_self dm_slfmgt_ed dm_drvs dm_hba1c dm_cntrld PHQ8 PHQ8_NR PHQ2 PHQ2_NR GAD2 GAD2_NR PHQ_DP_DIF PRB_SLEEP MED_BILS_COLLECT HC_CST_DELAY 
						TRB_GT_CARE GPS_SCR NOMD_CST MD_CARE_HLTH MD_TRVL_EASE SPC_ACCS_SAT QUAL_SAT INFO_SAT AFRD_RPLC_FOOD AFRD_EAT_HLTH NOAFRD_EAT_LESS NOAFRD_HUNGRY OWN_HOME SECT_EGHT HOME_VALU 
						LIVE_ALON N_HHLD CS_USC_XSEE_WGT SV_USC_XSEE_WGT TUP_SCR USOC USOC_PCP USOC_SPC SPC_SR PCP_SPC_COORD USOC_VS TEST_SR TEST_FLWP_SR NEED_HOME_SR HELP_HOME_SR RX_SR INFO_RX_SR HEAR_PRB HEAR_AID);
merge sample_&YEAR. vishear_&YEAR.;
by BASEID;
if CL_YR = &YEAR.;
run; 

/* Getting self-reported utilization information from accssmed 2017 and forward -*/
data accssmed_&YEAR. (keep = baseid CS_MED_XSEE_WGT SV_MED_XSEE_WGT SR_ED_VS_IND SR_OP_VS_IND SR_MD_VS_IND SR_MD_PCP_VS_IND SR_MD_SPC_VS_IND);
set svy_&YR.s.accssmed;

if ACCWT ne . then CS_MED_XSEE_WGT=ACCWT; else if CS_MED_XSEE_WGT=. then CS_MED_XSEE_WGT=ACSCWT;
if ACEWT ne . then SV_MED_XSEE_WGT=ACEWT; else if SV_MED_XSEE_WGT=. then SV_MED_XSEE_WGT=ACSEWT;

if D_ERVIST=1 then SR_ED_VS_IND=1; else SR_ED_VS_IND=0;
if D_OPVIST=1 then SR_OP_VS_IND=1; else SR_OP_VS_IND=0;
if D_MDVIST=1 then SR_MD_VS_IND=1; else SR_MD_VS_IND=0;
if D_MDVIST=1 and ( (MDSPCLTY in (8,10,12,13,16,24,37)) or (DRSPCLTY in (8,10,12,13,16,24,37)) )
then SR_MD_PCP_VS_IND=1; else SR_MD_PCP_VS_IND=0;
if D_MDVIST=1 and ( (MDSPCLTY in (1,2,3,5,7,9,11,14,15,17,18,19,20,21,22,25,26,27,28,29,30,31,32,33,34,35,36,38,39,91))
or  (DRSPCLTY in (1,2,3,5,7,9,11,14,15,17,18,19,20,21,22,25,26,27,28,29,30,31,32,33,34,35,36,38,39,91)) )
then SR_MD_SPC_VS_IND=1; else SR_MD_SPC_VS_IND=0;

run;

Data sample_&YEAR. (keep = BASEID CL_YR PTA_MM PTB_MM FFS_MM MA_MM MA_FLAG PRVSUP PRVSUP_MED PRVSUP_RX VA_FLAG H_MEDSTA NEW_BENE ESRD DISABLED OREC_AGE_ONLY MCAID_ANY MCAID_FULL MCAID_PART MC_ENT_ST MCR_ENT_YR MCR_ENT_MM SHSV_ACO1 NXGN_ACO CPCPL_PCMH PTD_MM 
						PD_FLAG PARTD_LIS HASPTD HASPTB HASHMO MA_INS_NM PTC_STR_RT MA_SNP MA_SNP_CD MA_SNP_DE MA_SNP_FIDE MA_SNP_IN H_DOB DOB AGE_0101 DIED DOD_YRMNDY AGE64 AGE74 AGE84 AGE85 USRD MBRZP MBRCTY MBRST SSA_SCTY RUCA1 ruca_metro1 ruca_micro1
						ruca_rural1 ADI_SCR NEWENGLAND MIDWEST WESTNORTHCENTRAL SOUTHATLANTIC EASTSCENTRAL WESTSCENTRAL MOUNTAIN PACIFIC MALE FEMALE WHITE BLACK HISPANIC ASIAN NTVAM HAWPAC MIXED OTHER 
						OTHR_LANG ENG_SPK_PRB EDUNKN EDNOHS EDHSGD EDSMCL EDCLGD INCOME FPL_LTE100 FPL_101_200 FPL_GT200 MARRIED WIDOW DIV_SEP NEV_MARRY VET_STS WORKING_SR MCBS_INST MDS3_INST NE MW SO WE HSPC 
						SNF_ST SNF_DY PTA_REIMB PTB_REIMB TOT_REIMB PTD_REIMB SHSV_ACO SV_VARSTRATUM SV_VARPSU SV_XSEE_WGT CS_VARSTRATUM CS_VARPSU CS_XSEE_WGT ALZDMT_SR DPRSSN_SR
						DM_RP DM_T1 DM_T2 DM_PRE HF_RP IH_RP ANGINA_RP CD_RP HYST_RP CLN_CNCR_RP IDD_RP PSYCH_RP ADL IADL COGIMP_SR DF_LIFT_RP DF_KNEL_RP DF_WALK_RP DF_DRES_RP DF_EAT_RP OVRWT OBESE HT_CM WT_KG BAD_SR_HLTH WRS_SR_HLTH 
						TBC_NOW PREV_SMKR ALC_PRB BLDCHL_RP BLDPRS_RP FLU_RP PNEUM_RP MAMM_RP PAPS_RP PSA_RP COL_FOBT_RP COL_FOBT_RP_LT5 COL_SCOP_RP COL_SCOP_RP_LT5 dm_ftexam dm_bldtst_self dm_slfmgt_ed dm_drvs dm_hba1c dm_cntrld PHQ8 PHQ8_NR PHQ2 PHQ2_NR GAD2 GAD2_NR PHQ_DP_DIF PRB_SLEEP MED_BILS_COLLECT HC_CST_DELAY 
						TRB_GT_CARE GPS_SCR NOMD_CST MD_CARE_HLTH MD_TRVL_EASE SPC_ACCS_SAT QUAL_SAT INFO_SAT AFRD_RPLC_FOOD AFRD_EAT_HLTH NOAFRD_EAT_LESS NOAFRD_HUNGRY OWN_HOME SECT_EGHT HOME_VALU 
						LIVE_ALON N_HHLD CS_USC_XSEE_WGT SV_USC_XSEE_WGT TUP_SCR USOC USOC_PCP USOC_SPC SPC_SR PCP_SPC_COORD USOC_VS TEST_SR TEST_FLWP_SR NEED_HOME_SR HELP_HOME_SR RX_SR INFO_RX_SR HEAR_PRB HEAR_AID CS_MED_XSEE_WGT SV_MED_XSEE_WGT SR_ED_VS_IND SR_OP_VS_IND SR_MD_VS_IND SR_MD_PCP_VS_IND SR_MD_SPC_VS_IND);
merge sample_&YEAR. accssmed_&YEAR;
by BASEID;
if CL_YR = &YEAR.;
run; 

/* 14 - Gettiing information from restmln, oasis, HH, and cost supplement data */ 

/* 14a - restmln */
data restmln_&YEAR. (keep = baseid LTC_days LTC_SNF_days);
set svy_&YR.s.restmln;

LTC_days = D_FDAYS; if LTC_days=. then LTC_Days=D_TFDAYS; /* This is valid for both MA and FFS benes */
LTC_SNF_days = LTC_days + D_SDAYS; /* This is valid for FFS benes only */

run;

/* 14b - oasis HH claims for FFS and MA benes*/
data oasis_&YEAR. (keep = baseid HH_vs_dt);
set svy_&YR.s.oasis;
HH_vs_dt=M0030_ST; 
run;

proc sort data = oasis_&YEAR. nodupkey; by baseid HH_vs_dt; run; 
/* noduping by baseid and HH visit date because the can have multiple rows for each HH visit */

data oasis_&YEAR. (keep = baseid CU_HH_CLM);
set oasis_&YEAR.;
CU_HH_CLM=1;
run;

proc summary data = oasis_&YEAR.;
by baseid;
var CU_HH_CLM;
output out=oasis_&YEAR.(keep=baseid CU_HH_CLM)sum=;
run;

/* 14c - home health visits for FFS */

data hh_&YEAR. (keep = BASEID HH_VS);
set claims&YR..hha;
HH_vs=CLM_HHA_TOT_VISIT_CNT;
run;

proc sort data = hh_&YEAR.; by baseid; run;

proc summary data = hh_&YEAR.;
by baseid;
var HH_vs;
output out=hh_&YEAR.(keep = baseid HH_VS)sum=;
run;

/* 14d - utilization from cost and use data for MA and FFS benes */

/* inpatient stays - so that I can compare FFS and MA beneficiaries on inpatient stays */
data ipe_&YEAR. (keep = baseid from thru CU_IP_ST CU_IP_DY);
set cs&YR.s.ipe;

EVBEGMM2 = put(EVBEGMM,z2.); format EVBEGMM2 z2.; EVBEGDD2 = put(EVBEGDD,z2.); format EVBEGDD2 z2.; EVBEGYY2 = put(EVBEGYY,z2.); format EVBEGYY2 z2.;
EVENDMM2 = put(EVENDMM,z2.); format EVENDMM2 z2.; EVENDDD2 = put(EVENDDD,z2.); format EVENDDD2 z2.; EVENDYY2 = put(EVENDYY,z2.); format EVENDYY2 z2.;
from_dt = EVBEGMM2||'-'||EVBEGDD2||'-'||EVBEGYY2; format FROM yymmdd10.; from = input(from_dt,MMDDYY8.); 
thru_dt = EVENDMM2||'-'||EVENDDD2||'-'||EVENDYY2; format THRU yymmdd10.; thru = input(thru_dt,MMDDYY8.); 
if thru > MDY(12,31,&YEAR.) then from = MDY(12,31,&YEAR.); /* capping the days at the end of the year */

CU_IP_ST=1; /* counting hospital stays */
CU_IP_DY = (thru - from) + 1;

run;

proc sort data = ipe_&YEAR.; by baseid; run;

proc summary data = ipe_&YEAR.;
by baseid;
var CU_IP_ST CU_IP_DY;
output out = ipe_&YEAR. (keep = baseid CU_IP_ST CU_IP_DY)sum=;
run;

/* SNF stays - so that I can compare FFS and MA beneficiaries on inpatient stays */
data iue_&YEAR. (keep = baseid from thru CU_SNF_ST CU_SNF_DY);
set cs&YR.s.iue;

EVBEGMM2 = put(EVBEGMM,z2.); format EVBEGMM2 z2.; EVBEGDD2 = put(EVBEGDD,z2.); format EVBEGDD2 z2.; EVBEGYY2 = put(EVBEGYY,z2.); format EVBEGYY2 z2.;
EVENDMM2 = put(EVENDMM,z2.); format EVENDMM2 z2.; EVENDDD2 = put(EVENDDD,z2.); format EVENDDD2 z2.; EVENDYY2 = put(EVENDYY,z2.); format EVENDYY2 z2.;
from_dt = EVBEGMM2||'-'||EVBEGDD2||'-'||EVBEGYY2; format FROM yymmdd10.; from = input(from_dt,MMDDYY8.); 
thru_dt = EVENDMM2||'-'||EVENDDD2||'-'||EVENDYY2; format THRU yymmdd10.; thru = input(thru_dt,MMDDYY8.); 
if thru > MDY(12,31,&YEAR.) then from = MDY(12,31,&YEAR.); /* capping the days at the end of the year */

CU_SNF_ST=1; /* counting hospital stays */
CU_SNF_DY = (thru - from) + 1;

run;

proc sort data = iue_&YEAR.; by baseid; run;

proc summary data = iue_&YEAR.;
by baseid;
var CU_SNF_ST CU_SNF_DY;
output out = iue_&YEAR. (keep = baseid CU_SNF_ST CU_SNF_DY)sum=;
run;

/* ED visits from outpatient hospital events file */
data ope_&YEAR. (keep = baseid CU_ED_VS);
set cs&YR.s.ope;

if OREVTYPE='ER';
CU_ED_VS=1;
run;

proc sort data = ope_&YEAR.; by baseid; run;

proc summary data = ope_&YEAR.;
by baseid;
var CU_ED_VS;
output out = ope_&YEAR. (keep = baseid CU_ED_VS)sum=;
run;

/* clinician visits from medical provider events file */
data mpe_&YEAR. (keep = baseid CU_CLN_VS);
set cs&YR.s.mpe;

if EVNTTYPE='MP';
CU_CLN_VS=1;
run;

proc sort data = mpe_&YEAR.; by baseid; run;

proc summary data = mpe_&YEAR.;
by baseid;
var CU_CLN_VS;
output out = mpe_&YEAR. (keep = baseid CU_CLN_VS)sum=;
run;

/* total costs paid by Medicaid from the person summary file */
data ps_&YEAR. (keep = baseid MCAID_REIMB);
set cs&YR.s.ps;
MCAID_REIMB = PAMTCAID; if MCAID_REIMB=. then MCAID_REIMB=0;
run;

/* RX fill dates and days supply for psychotherapeutic and anti-dementia drugs - use this for my depression & dementia studies */
data pme_&YEAR. (keep = baseid rx_fill_dt days_suply_num rx_ther_class);
set cs&YR.s.pme;

rx_fill_dt=SERV_DT;
days_suply_num=DAYSUPP;

/* searching through all the brand and generic names of ADRD targeted drugs - as long as the field 'contains' those names:
brand names of ADRD targeted drugs: 'ARICEPT','EXELON','REMINYL','RAZADYNE','NAMENDA'
generic names of ADRD targeted drugs: 'DONEPEZIL','RIVASTIGMINE','GALANTAMINE','MEMANTINE','NAMZARIC' */
if prxmatch('/ARICEPT|EXELON|REMINYL|RAZADYNE|NAMENDA|DONEPEZIL|RIVASTIGMINE|GALANTAMINE|MEMANTINE|NAMZARIC/i', FDB_GNN) 
or prxmatch('/ARICEPT|EXELON|REMINYL|RAZADYNE|NAMENDA|DONEPEZIL|RIVASTIGMINE|GALANTAMINE|MEMANTINE|NAMZARIC/i', FDB_BN)
or prxmatch('/ARICEPT|EXELON|REMINYL|RAZADYNE|NAMENDA|DONEPEZIL|RIVASTIGMINE|GALANTAMINE|MEMANTINE|NAMZARIC/i', DRUGNAME)
then rx_ther_class='adrd';

/* searching through all generic names of anti-depressants from 2020 NCQA HEDIS specs */
else if prxmatch('/NORTRIPTYLINE|PAROXETINE|PHENELZINE|PROTRIPTYLINE|SELEGILINE|SERTRALINE|TRANYLCYPROMINE|TRAZODONE|TRIMIPRAMINE|VENLAFAXINE|VILAZODONE|VORTIOXETINE/i', FDB_GNN)
or prxmatch('/NORTRIPTYLINE|PAROXETINE|PHENELZINE|PROTRIPTYLINE|SELEGILINE|SERTRALINE|TRANYLCYPROMINE|TRAZODONE|TRIMIPRAMINE|VENLAFAXINE|VILAZODONE|VORTIOXETINE/i', FDB_BN)
or prxmatch('/NORTRIPTYLINE|PAROXETINE|PHENELZINE|PROTRIPTYLINE|SELEGILINE|SERTRALINE|TRANYLCYPROMINE|TRAZODONE|TRIMIPRAMINE|VENLAFAXINE|VILAZODONE|VORTIOXETINE/i', DRUGNAME)
then rx_ther_class='andp';

/* searching through all generic names of anti-depressants from 2020 NCQA HEDIS specs */
else if prxmatch('/AMITRIPTYLINE|AMOXAPINE|BUPROPION|CITALOPRAM|CLOMIPRAMINE|DESIPRAMINE|DESVENLAFAXINE|DOXEPIN|DULOXETINE|ESCITALOPRAM|FLUOXETINE|FLUVOXAMINE|IMIPRAMINE|ISOCARBOXAZID|LEVOMILNACIPRAN|MAPROTILINE|MIRTAZAPINE|NEFAZODONE/i', FDB_GNN)
or prxmatch('/AMITRIPTYLINE|AMOXAPINE|BUPROPION|CITALOPRAM|CLOMIPRAMINE|DESIPRAMINE|DESVENLAFAXINE|DOXEPIN|DULOXETINE|ESCITALOPRAM|FLUOXETINE|FLUVOXAMINE|IMIPRAMINE|ISOCARBOXAZID|LEVOMILNACIPRAN|MAPROTILINE|MIRTAZAPINE|NEFAZODONE/i', FDB_BN)
or prxmatch('/AMITRIPTYLINE|AMOXAPINE|BUPROPION|CITALOPRAM|CLOMIPRAMINE|DESIPRAMINE|DESVENLAFAXINE|DOXEPIN|DULOXETINE|ESCITALOPRAM|FLUOXETINE|FLUVOXAMINE|IMIPRAMINE|ISOCARBOXAZID|LEVOMILNACIPRAN|MAPROTILINE|MIRTAZAPINE|NEFAZODONE/i', DRUGNAME)
then rx_ther_class='andp';

/*else if THERCC='80' then rx_ther_class='psyc';  /* This is the F.D.B. generic therapeutic class for Psychotherapeutic drugs */
/*else rx_ther_class='';*/
run;

data rx_&YEAR. (keep= baseid rx_fill_dt days_suply_num rx_ther_class);
set pme_&YEAR.;
if rx_ther_class ne '' and rx_fill_dt ne .;
run;

data inp_&YEAR. (keep = baseid clm_from_dt clm_thru_dt);
set claims&YR..inp;
run; 

%MACRO inpt_PDC_Calculate(rxDSN= /* Input Rx data set name */ 
, pat_id = /* Patient ID */ 
, Rx_fill_dt = /* Rx fill date */ 
, rx_class = /* For multi rx user */ 
, inptDSN = /* Input Inpatient dataset name */ 
, inpt_begin_dt = /* Inpatient addmission date */ 
, inpt_end_dt = /* Inpatinet discharge date */ 
, outdsn = /* Output dataset name */ 
, Study_end_dt = /* Study Ending date. example Dec312012 */ 
); 
/* Preparing the Rx data, create next fill date variable by using Lag funcation and proc sort*/ 
Proc sort data=&rxDsn out= rx_sorted nodupkey; 
by &pat_id &rx_class DESCENDING &rx_fill_dt ; 
RUN; 
Data RX_dates; 
set rx_sorted; 
by &pat_id &rx_class; 
next_srvc_dt = lag(&Rx_fill_dt); 
if first.&rx_class then next_srvc_dt = .; 
format next_srvc_dt date9.; 
RUN; 
Proc sort data=RX_dates ; 
by &pat_id &rx_class &rx_fill_dt ; 
RUN; 
/* Create service ending date of each prescription fill */ 
Data RX_dates2; 
set RX_dates; 
format rx_end_dt date9.; 
retain Rx_leftover ; 
by &pat_id &rx_class &rx_fill_dt ; 

If first.&rx_class then do; 
if &rx_fill_dt + days_suply_num < next_srvc_dt then do; 
rx_end_dt = &rx_fill_dt + days_suply_num; 
rx_leftover = 0; 
end; else do; 
rx_end_dt = next_srvc_dt -1; 
rx_leftover = (&rx_fill_dt + days_suply_num) - next_srvc_dt + 1; 
end; 
end; else if last.&rx_class then do; 
rx_end_dt = &rx_fill_dt + rx_leftover + days_suply_num; 
rx_leftover = 0; 
end; else do; 
if &rx_fill_dt + rx_leftover + days_suply_num <next_srvc_dt then do; 
rx_end_dt = &rx_fill_dt + rx_leftover + days_suply_num; 
rx_leftover = 0; 
end; else do; 
rx_end_dt = next_srvc_dt -1; 
rx_leftover = (&rx_fill_dt + rx_leftover + days_suply_num) - next_srvc_dt +1 ; 
end; 
end; 
if first.&rx_class and last.&rx_class then rx_end_dt = &rx_fill_dt + + days_suply_num; 
if next_srvc_dt = . then next_srvc_dt = rx_end_dt + 1; 
RUN; 

Proc sort data=&InptDSN out=inpt_stay; 
by &pat_id &inpt_begin_dt; 
RUN; 
/* Adjust the inpatient stay days */ 
Data adj_rx_date; 
length &pat_id. $8. &inpt_begin_dt. &inpt_end_dt. 8.; 
format &inpt_begin_dt. &inpt_end_dt. adj_rx_end_dt date9. ; 
if _N_ = 1 then do; /*Define the Hash table and put the inpatient stay dataset */ 
dcl hash inpt(dataset: "&inptDSN", multidata: 'Y', hashexp: 20, ordered: 'a'); 
inpt.defineKey("&pat_id"); 
inpt.definedata("&inpt_begin_dt", "&inpt_end_dt"); 
inpt.definedone(); 
Call missing(&pat_id, &inpt_begin_dt, &inpt_end_dt); 
End; 
set RX_dates2; 
by &pat_id &rx_class &Rx_fill_dt; 
retain numRx_leftover_4inpt censored_days; 
if first.&pat_id then do; 
numRx_leftover_4inpt = 0; 
censored_days = 0; 
end; 
rc = inpt.find(); 
If rc NE 0 then do; 
output; 
goto exist; 
End; else do; 
if (&rx_fill_dt <&inpt_begin_dt < rx_end_dt) and (&rx_fill_dt <&inpt_end_dt < rx_end_dt) 
then do; 
numRx_leftover_4inpt = &inpt_end_dt - &inpt_begin_dt; 
censored_days = &inpt_end_dt - &inpt_begin_dt; 
if (rx_end_dt + numRx_leftover_4inpt) < next_srvc_dt then do; 
adj_rx_end_dt = rx_end_dt + numRx_leftover_4inpt; 
numRx_leftover_4inpt = 0; 
end;else do; 
numRx_leftover_4inpt = (rx_end_dt + numRx_leftover_4inpt) - next_srvc_dt; 
adj_rx_end_dt = next_srvc_dt -1 ; 
end; 
end; 
if &rx_fill_dt <&inpt_begin_dt < rx_end_dt and &inpt_end_dt >= rx_end_dt then do; 
censored_days = rx_end_dt - &inpt_begin_dt; 
numRx_leftover_4inpt = rx_end_dt - &inpt_begin_dt; 
if &inpt_end_dt < next_srvc_dt then do; 
if numRx_leftover_4inpt < next_srvc_dt - &inpt_end_dt then do; 
adj_rx_end_dt = &inpt_end_dt + numRx_leftover_4inpt; 
numRx_leftover_4inpt = 0 ; 
end;else do; 
numRx_leftover_4inpt = numRx_leftover_4inpt -(next_srvc_dt - &inpt_end_dt); 
adj_rx_end_dt = next_srvc_dt -1; 
end; 
end; else do; 
numRx_leftover_4inpt = numRx_leftover_4inpt; 
adj_rx_end_dt = next_srvc_dt -1; 
end; 
end; 
End; 
rnext = inpt.find_next(); 
if rnext NE 0 then output; 
do while(rnext = 0); 
if (&rx_fill_dt <&inpt_begin_dt < rx_end_dt) and (&rx_fill_dt <&inpt_end_dt < rx_end_dt) then do; 
numRx_leftover_4inpt = &inpt_end_dt - &inpt_begin_dt; 
censored_days = &inpt_end_dt - &inpt_begin_dt + censored_days; 
if (rx_end_dt + numRx_leftover_4inpt) < next_srvc_dt then do; 
adj_rx_end_dt = rx_end_dt + numRx_leftover_4inpt; 
numRx_leftover_4inpt = 0; 
end;else do; 
numRx_leftover_4inpt = (rx_end_dt + numRx_leftover_4inpt) - next_srvc_dt; 
adj_rx_end_dt = next_srvc_dt -1 ; 
end; 
end; 
if &rx_fill_dt <&inpt_begin_dt < rx_end_dt and &inpt_end_dt >= rx_end_dt then do; 
censored_days = rx_end_dt - &inpt_begin_dt + censored_days ; 
numRx_leftover_4inpt = rx_end_dt - &inpt_begin_dt; 
if &inpt_end_dt < next_srvc_dt then do; 
if numRx_leftover_4inpt < next_srvc_dt - &inpt_end_dt then do; 
adj_rx_end_dt = &inpt_end_dt + numRx_leftover_4inpt; 
numRx_leftover_4inpt = 0 ; 
end;else do; 
numRx_leftover_4inpt = numRx_leftover_4inpt -(next_srvc_dt - &inpt_end_dt); 
adj_rx_end_dt = next_srvc_dt -1; 
end; 
end; else do; 
numRx_leftover_4inpt = numRx_leftover_4inpt; 
adj_rx_end_dt = next_srvc_dt -1; 
end; 
end; 
inpt.has_next(result: r) ; 
if r = 0 then output ; 
rnext = inpt.find_next(); 
end; 
drop r rc rnext; 
exist: ; 
RUN; 
PROC SORT DATA = adj_rx_date; BY &pat_id &rx_fill_dt &rx_class; RUN;
/* create total inpatient stay days */ 
Data Inpt_days; 
set adj_rx_date ; 
by &pat_id &rx_fill_dt &rx_class; 
if last.&pat_id ; 
RUN; 
/* create total medication covered days */ 
Data Rx_covered_days; 
set adj_rx_date; 
if adj_rx_end_dt = . then adj_rx_end_dt = rx_end_dt; 
rx_covered_days = min(adj_rx_end_dt, "&Study_end_dt"d) - &rx_fill_dt; 
RUN; 
Proc sql; 
create table tot_Rx_covered_days 
as select distinct &pat_id 
, &rx_class 
, min(&rx_fill_dt) as period_start format = date9. 
, max(adj_rx_end_dt) as period_end format = date9. 
, sum(rx_covered_days)as tot_rx_covered_days 
from rx_covered_days 
group by &pat_id, &rx_class ; 

create table pre_pdc 
as select a.* 
, b.censored_days 
from tot_Rx_covered_days as a left join inpt_days as b 
on a.&pat_id = b.&pat_id; 
Quit; 

/* Calculate the pdc */ 
Data &outdsn; 
set pre_pdc; 
denominator = "&Study_end_dt"d - period_start - censored_days; 
if denominator = 0 then PDC = 0; 
else PDC = tot_rx_covered_days/denominator; 
RUN; 
%MEND; 

/* Macro call */ 
%inpt_PDC_Calculate(rxDSN = rx_&YEAR. 
, pat_id = baseid 
, Rx_fill_dt = rx_fill_dt 
, rx_class = rx_ther_class 
, inptDSN = inp_&YEAR.
, inpt_begin_dt = clm_from_dt 
, inpt_end_dt = clm_thru_dt 
, outdsn = pdc_&YEAR.
, Study_end_dt = 31Dec&Year. );

/*Data psych_pdc_&Year. (keep = baseid RX_PSYCH_COV_DYS RX_PSYCH_PDC);*/
/*set pdc_&Year.;*/
/*if rx_ther_class='psyc'; */
/*RX_PSYCH_COV_DYS=tot_rx_covered_days; */
/*RX_PSYCH_PDC=pdc;*/
/*run;*/

Data adrd_pdc_&Year. (keep = baseid RX_ADRD_COV_DYS RX_ADRD_PDC);
set pdc_&Year.;
if rx_ther_class='adrd'; 
RX_ADRD_COV_DYS=tot_rx_covered_days; 
RX_ADRD_PDC=pdc;
run;

Data andp_pdc_&Year. (keep = baseid RX_ANDP_COV_DYS RX_ANDP_PDC);
set pdc_&Year.;
if rx_ther_class='andp'; 
RX_ANDP_COV_DYS=tot_rx_covered_days; 
RX_ANDP_PDC=pdc;
run;

Data sample_&YEAR. (keep = BASEID CL_YR PTA_MM PTB_MM FFS_MM MA_MM MA_FLAG PRVSUP PRVSUP_MED PRVSUP_RX VA_FLAG H_MEDSTA NEW_BENE ESRD DISABLED OREC_AGE_ONLY MCAID_ANY MCAID_FULL MCAID_PART MC_ENT_ST MCR_ENT_YR MCR_ENT_MM SHSV_ACO1 NXGN_ACO CPCPL_PCMH PTD_MM 
						PD_FLAG PARTD_LIS HASPTD HASPTB HASHMO MA_INS_NM PTC_STR_RT MA_SNP MA_SNP_CD MA_SNP_DE MA_SNP_FIDE MA_SNP_IN H_DOB DOB AGE_0101 DIED DOD_YRMNDY AGE64 AGE74 AGE84 AGE85 USRD MBRZP MBRCTY MBRST SSA_SCTY RUCA1 ruca_metro1 ruca_micro1
						ruca_rural1 ADI_SCR NEWENGLAND MIDWEST WESTNORTHCENTRAL SOUTHATLANTIC EASTSCENTRAL WESTSCENTRAL MOUNTAIN PACIFIC MALE FEMALE WHITE BLACK HISPANIC ASIAN NTVAM HAWPAC MIXED OTHER 
						OTHR_LANG ENG_SPK_PRB EDUNKN EDNOHS EDHSGD EDSMCL EDCLGD INCOME FPL_LTE100 FPL_101_200 FPL_GT200 MARRIED WIDOW DIV_SEP NEV_MARRY VET_STS WORKING_SR MCBS_INST MDS3_INST NE MW SO WE HSPC 
						SNF_ST SNF_DY PTA_REIMB PTB_REIMB TOT_REIMB PTD_REIMB SHSV_ACO SV_VARSTRATUM SV_VARPSU SV_XSEE_WGT CS_VARSTRATUM CS_VARPSU CS_XSEE_WGT ALZDMT_SR DPRSSN_SR
						DM_RP DM_T1 DM_T2 DM_PRE HF_RP IH_RP ANGINA_RP CD_RP HYST_RP CLN_CNCR_RP IDD_RP PSYCH_RP ADL IADL COGIMP_SR DF_LIFT_RP DF_KNEL_RP DF_WALK_RP DF_DRES_RP DF_EAT_RP OVRWT OBESE HT_CM WT_KG BAD_SR_HLTH WRS_SR_HLTH 
						TBC_NOW PREV_SMKR ALC_PRB BLDCHL_RP BLDPRS_RP FLU_RP PNEUM_RP MAMM_RP PAPS_RP PSA_RP COL_FOBT_RP COL_FOBT_RP_LT5 COL_SCOP_RP COL_SCOP_RP_LT5 dm_ftexam dm_bldtst_self dm_slfmgt_ed dm_drvs dm_hba1c dm_cntrld PHQ8 PHQ8_NR PHQ2 PHQ2_NR GAD2 GAD2_NR PHQ_DP_DIF PRB_SLEEP MED_BILS_COLLECT HC_CST_DELAY 
						TRB_GT_CARE GPS_SCR NOMD_CST MD_CARE_HLTH MD_TRVL_EASE SPC_ACCS_SAT QUAL_SAT INFO_SAT AFRD_RPLC_FOOD AFRD_EAT_HLTH NOAFRD_EAT_LESS NOAFRD_HUNGRY OWN_HOME SECT_EGHT HOME_VALU 
						LIVE_ALON N_HHLD CS_USC_XSEE_WGT SV_USC_XSEE_WGT TUP_SCR USOC USOC_PCP USOC_SPC SPC_SR PCP_SPC_COORD USOC_VS TEST_SR TEST_FLWP_SR NEED_HOME_SR HELP_HOME_SR RX_SR INFO_RX_SR HEAR_PRB HEAR_AID CS_MED_XSEE_WGT SV_MED_XSEE_WGT SR_ED_VS_IND SR_OP_VS_IND SR_MD_VS_IND SR_MD_PCP_VS_IND SR_MD_SPC_VS_IND 
						LTC_days LTC_SNF_days HH_VS CU_HH_CLM CU_IP_ST CU_IP_DY CU_SNF_ST CU_SNF_DY CU_ED_VS CU_CLN_VS MCAID_REIMB RX_ADRD_COV_DYS RX_ADRD_PDC RX_ANDP_COV_DYS RX_ANDP_PDC);
merge sample_&YEAR. restmln_&YEAR. oasis_&YEAR. hh_&YEAR. ipe_&YEAR. iue_&YEAR. ope_&YEAR. mpe_&YEAR. ps_&YEAR. adrd_pdc_&Year. andp_pdc_&Year.;
by BASEID;
if CL_YR = &YEAR.;

if LTC_days=. then LTC_days=0;
if LTC_SNF_days=. then LTC_SNF_days=0;
if HH_VS=. then HH_VS=0;
if CU_HH_CLM=. then CU_HH_CLM=0;
if CU_IP_ST=. then CU_IP_ST=0;
if CU_IP_ST=0 then CU_IP_DY=0;
if CU_SNF_ST=. then CU_SNF_ST=0;
if CU_SNF_ST=0 then CU_SNF_DY=0;
if CU_ED_VS=. then CU_ED_VS=0;
if CU_CLN_VS=. then CU_CLN_VS=0;
if MCAID_REIMB=. then MCAID_REIMB=0;
/*if RX_PSYCH_COV_DYS=. then RX_PSYCH_COV_DYS=0;*/
/*if RX_PSYCH_PDC=. then RX_PSYCH_PDC=0;*/
if RX_ADRD_COV_DYS=. then RX_ADRD_COV_DYS=0;
if RX_ADRD_PDC=. then RX_ADRD_PDC=0;
if RX_ANDP_COV_DYS=. then RX_ANDP_COV_DYS=0;
if RX_ANDP_PDC=. then RX_ANDP_PDC=0;

run; 

/* 15 - Getting information from chrncdfl */
data chrncdfl_&YEAR. (keep = BASEID AD_DX_CCW AD_DX_PRYR AMI_DX CD_DX AFB_DX BRC_DX CRC_DX ENC_DX LUC_DX PRC_DX HF_DX CKD_DX DP_DX_CCW DP_DX_CCW_DT SPDP_DX_CCW DM_DX DSL_DX
							 HTN_DX IH_DX ART_DX SRK_DX ANX_DX_CCW ANX_DX_CCW_DT SP_DX_CCW HIV_DX ID_DX_CCW);
set svy_&YR.s.chrncdfl;

if ALZHDMTA in (1,3) or ALZH in (1,3) then AD_DX_CCW=1; else AD_DX_CCW=0;
if (ALZHDMTE ne . and (ALZHDMTE < (MDY(01,01,&YEAR.)))) or (ALZHE ne . and (ALZHE < (MDY(01,01,&YEAR.)))) then AD_DX_PRYR=1; else AD_DX_PRYR=0;
if AMI in (1,3) then AMI_DX=1; else AMI_DX=0;
if ASTHMA in (1,3) or COPD in (1,3) then CD_DX=1; else CD_DX=0;
if ATRIALFB in (1,3) then AFB_DX=1; else AFB_DX=0;
if CNCRBRST in (1,3) then BRC_DX=1; else BRC_DX=0;
if CNCRCLRC in (1,3) then CRC_DX=1; else CRC_DX=0;
if CNCRENDM in (1,3) then ENC_DX=1; else ENC_DX=0;
if CNCRLUNG in (1,3) then LUC_DX=1; else LUC_DX=0;
if CNCRPRST in (1,3) then PRC_DX=1; else PRC_DX=0;
if CHF in (1,3) then HF_DX=1; else HF_DX=0;
if CHRNKIDN  in (1,3) then CKD_DX=1; else CKD_DX=0;
if DEPRESSN in (1,3) or BIPL in (1,3) or DEPSN in (1,3) then DP_DX_CCW=1; else DP_DX_CCW=0;
DP_DX_CCW_DT = min(DEPRSSNE, BIPLE, DEPSNE); format DP_DX_CCW_DT mmddyy10.;
if BIPL in (1,3) or DEPSN in (1,3) then SPDP_DX_CCW=1; else SPDP_DX_CCW=0;
if DIABETES in (1,3) then DM_DX=1; else DM_DX=0;
if HYPERL in (1,3) then DSL_DX=1; else DSL_DX=0;
if HYPERT in (1,3) then HTN_DX=1; else HTN_DX=0;
if ISCHMCHT in (1,3) then IH_DX=1; else IH_DX=0;
if RA_OA in (1,3) then ART_DX=1; else ART_DX=0;
if STRKETIA in (1,3) then SRK_DX=1; else SRK_DX=0;
if ANXI in (1,3) then ANX_DX_CCW=1; else ANX_DX_CCW=0;
ANX_DX_CCW_DT = ANXIE; format ANX_DX_CCW_DT mmddyy10.;
if SCHI in (1,3) or SCHIOT in (1,3) then SP_DX_CCW=1; else SP_DX_CCW=0;
if HIVAIDS in (1,3) then HIV_DX=1; else HIV_DX=0;
if INTDIS in (1,3) then ID_DX_CCW=1; else ID_DX_CCW=0;

run;

Data sample_&YEAR. (keep = BASEID CL_YR PTA_MM PTB_MM FFS_MM MA_MM MA_FLAG PRVSUP PRVSUP_MED PRVSUP_RX VA_FLAG H_MEDSTA NEW_BENE ESRD DISABLED OREC_AGE_ONLY MCAID_ANY MCAID_FULL MCAID_PART MC_ENT_ST MCR_ENT_YR MCR_ENT_MM SHSV_ACO1 NXGN_ACO CPCPL_PCMH PTD_MM 
						PD_FLAG PARTD_LIS HASPTD HASPTB HASHMO MA_INS_NM PTC_STR_RT MA_SNP MA_SNP_CD MA_SNP_DE MA_SNP_FIDE MA_SNP_IN H_DOB DOB AGE_0101 DIED DOD_YRMNDY AGE64 AGE74 AGE84 AGE85 USRD MBRZP MBRCTY MBRST SSA_SCTY RUCA1 ruca_metro1 ruca_micro1
						ruca_rural1 ADI_SCR NEWENGLAND MIDWEST WESTNORTHCENTRAL SOUTHATLANTIC EASTSCENTRAL WESTSCENTRAL MOUNTAIN PACIFIC MALE FEMALE WHITE BLACK HISPANIC ASIAN NTVAM HAWPAC MIXED OTHER 
						OTHR_LANG ENG_SPK_PRB EDUNKN EDNOHS EDHSGD EDSMCL EDCLGD INCOME FPL_LTE100 FPL_101_200 FPL_GT200 MARRIED WIDOW DIV_SEP NEV_MARRY VET_STS WORKING_SR MCBS_INST MDS3_INST NE MW SO WE HSPC 
						SNF_ST SNF_DY PTA_REIMB PTB_REIMB TOT_REIMB PTD_REIMB SHSV_ACO SV_VARSTRATUM SV_VARPSU SV_XSEE_WGT CS_VARSTRATUM CS_VARPSU CS_XSEE_WGT ALZDMT_SR DPRSSN_SR
						DM_RP DM_T1 DM_T2 DM_PRE HF_RP IH_RP ANGINA_RP CD_RP HYST_RP CLN_CNCR_RP IDD_RP PSYCH_RP ADL IADL COGIMP_SR DF_LIFT_RP DF_KNEL_RP DF_WALK_RP DF_DRES_RP DF_EAT_RP OVRWT OBESE HT_CM WT_KG BAD_SR_HLTH WRS_SR_HLTH 
						TBC_NOW PREV_SMKR ALC_PRB BLDCHL_RP BLDPRS_RP FLU_RP PNEUM_RP MAMM_RP PAPS_RP PSA_RP COL_FOBT_RP COL_FOBT_RP_LT5 COL_SCOP_RP COL_SCOP_RP_LT5 dm_ftexam dm_bldtst_self dm_slfmgt_ed dm_drvs dm_hba1c dm_cntrld PHQ8 PHQ8_NR PHQ2 PHQ2_NR GAD2 GAD2_NR PHQ_DP_DIF PRB_SLEEP MED_BILS_COLLECT HC_CST_DELAY 
						TRB_GT_CARE GPS_SCR NOMD_CST MD_CARE_HLTH MD_TRVL_EASE SPC_ACCS_SAT QUAL_SAT INFO_SAT AFRD_RPLC_FOOD AFRD_EAT_HLTH NOAFRD_EAT_LESS NOAFRD_HUNGRY OWN_HOME SECT_EGHT HOME_VALU 
						LIVE_ALON N_HHLD CS_USC_XSEE_WGT SV_USC_XSEE_WGT TUP_SCR USOC USOC_PCP USOC_SPC SPC_SR PCP_SPC_COORD USOC_VS TEST_SR TEST_FLWP_SR NEED_HOME_SR HELP_HOME_SR RX_SR INFO_RX_SR HEAR_PRB HEAR_AID CS_MED_XSEE_WGT SV_MED_XSEE_WGT SR_ED_VS_IND SR_OP_VS_IND SR_MD_VS_IND SR_MD_PCP_VS_IND SR_MD_SPC_VS_IND LTC_days LTC_SNF_days HH_VS CU_HH_CLM CU_IP_ST CU_IP_DY CU_SNF_ST CU_SNF_DY CU_ED_VS CU_CLN_VS MCAID_REIMB RX_ADRD_COV_DYS RX_ADRD_PDC
						RX_ANDP_COV_DYS RX_ANDP_PDC AD_DX_CCW AD_DX_PRYR AMI_DX CD_DX AFB_DX BRC_DX CRC_DX ENC_DX LUC_DX PRC_DX HF_DX CKD_DX DP_DX_CCW DP_DX_CCW_DT SPDP_DX_CCW DM_DX DSL_DX HTN_DX IH_DX ART_DX SRK_DX ANX_DX_CCW ANX_DX_CCW_DT SP_DX_CCW HIV_DX ID_DX_CCW);
merge sample_&YEAR. chrncdfl_&YEAR.;
by BASEID;
if CL_YR = &YEAR.;
run; 


/* 16 - claims diagnoses for conditions using the CMS chronic condition warehouse claims algorithms */
/* identify # of encounters (=unique patient, Date of Service visits) for inpatient, snf, hh, outpatient, physician 
		- for peropheral vascular disease, alcoholism, drug use, autism, cerebral palsy  */

Data hha_&YEAR. (keep = BASEID CLM_FROM_DT CLM_THRU_DT CLM_HHA_TOT_VISIT_CNT PRNCPAL_DGNS_CD ICD_DGNS_CD1 ICD_DGNS_CD2 ICD_DGNS_CD3 ICD_DGNS_CD4 ICD_DGNS_CD5 ICD_DGNS_CD6 ICD_DGNS_CD7
						ICD_DGNS_CD8 ICD_DGNS_CD9 ICD_DGNS_CD10 ICD_DGNS_CD11 ICD_DGNS_CD12 ICD_DGNS_CD13 ICD_DGNS_CD14 ICD_DGNS_CD15 ICD_DGNS_CD16 ICD_DGNS_CD17 ICD_DGNS_CD18
						ICD_DGNS_CD19 ICD_DGNS_CD20 ICD_DGNS_CD21 ICD_DGNS_CD22 ICD_DGNS_CD23 ICD_DGNS_CD24 ICD_DGNS_CD25 
						pvd_hh_clm alc_hh_clm drg_hh_clm id_hh_clm au_hh_clm cp_hh_clm ad_hh_clm dep_hh_clm spdep_hh_clm anx_hh_clm);
set claims&YR..hha;
pvd_hh_clm=0;
alc_hh_clm=0;
drg_hh_clm=0;
id_hh_clm=0;
au_hh_clm=0;
cp_hh_clm=0;
ad_hh_clm=0;
dep_hh_clm=0;
spdep_hh_clm=0;
anx_hh_clm=0;
array diag{25} ICD_DGNS_CD1-ICD_DGNS_CD25;
do i=1 to 25;

/* peripheral vascular disease */
  if diag{i} in ('E0851','E0852','E0951','E0952','E1051','E1052','E1151','E1152','E1351','E1352','I700','I701','I70201','I70202','I70203','I70208','I70209','I70211',
'I70212','I70213','I70218','I70219','I70221','I70222','I70223','I70228','I70229','I70231','I70232','I70233','I70234','I70235','I70238','I70239','I70241','I70242','I70243',
'I70244','I70245','I70248','I70249','I7025','I70291','I70292','I70293','I70298','I70299','I7092','I7381','I7389','I739','I791','I798')
  then pvd_hh_clm=CLM_HHA_TOT_VISIT_CNT; 
/* Alcoholism */
  if diag{i} in ('F1010','F10120','F10121','F10129','F1014','F10150','F10151','F10159','F10180','F10181','F10182','F10188','F1019','F1020','F10220','F10221','F10229','F10230','F10231',
'F10232','F10239','F1024','F10250','F10251','F10259','F1026','F1027','F10280','F10281','F10282','F10288','F1029','F10920','F10921','F10929','F1094','F10950','F10951','F10959',
'F1096','F1097','F10980','F10981','F10982','F10988','F1099','G621','I426','K2920','K2921','K700','K7010','K7011','K702','K7030','K7031','K7040','K7041','K709','P043','Q860',
'T510X1A','T510X2A','T510X3A','T510X4A','Z7141','Z7142') 														
  then alc_hh_clm=CLM_HHA_TOT_VISIT_CNT; 
/* Drug Use Disorder */
  if diag{i} in ('F1110','F11120','F11121','F11122','F11129','F1114','F11150','F11151','F11159','F11181','F11182','F11188','F1119','F1120','F11220','F11221',
'F11222','F11229','F1123','F1124','F11250','F11251','F11259','F11281','F11282','F11288','F1129','F1190','F11920','F11921','F11922','F11929',
'F1193','F1194','F11950','F11951','F11959','F11981','F11982','F11988','F1199','F1210','F12120','F12121','F12122','F12129','F12150','F12151',
'F12159','F12180','F12188','F1219','F1220','F12220','F12221','F12222','F12229','F12250','F12251','F12259','F12280','F12288','F1229','F1290',
'F12920','F12921','F12922','F12929','F12950','F12951','F12959','F12980','F12988','F1299','F1310','F13120','F13121','F13129','F1314','F13150',
'F13151','F13159','F13180','F13181','F13182','F13188','F1319','F1320','F13220','F13221','F13229','F13230','F13231','F13232','F13239','F1324',
'F13250','F13251','F13259','F1326','F1327','F13280','F13281','F13282','F13288','F1329','F1390','F13920','F13921','F13929','F13930','F13931',
'F13932','F13939','F1394','F13950','F13951','F13959','F1396','F1397','F13980','F13981','F13982','F13988','F1399','F1410','F14120','F14121',
'F14122','F14129','F1414','F14150','F14151','F14159','F14180','F14181','F14182','F14188','F1419','F1420','F14220','F14221','F14222','F14229',
'F1423','F1424','F14250','F14251','F14259','F14280','F14281','F14282','F14288','F1429','F1490','F14920','F14921','F14922','F14929','F1494',
'F14950','F14951','F14959','F14980','F14981','F14982','F14988','F1499','F1510','F15120','F15121','F15122','F15129','F1514','F15150','F15151',
'F15159','F15180','F15181','F15182','F15188','F1519','F1520','F15220','F15221','F15222','F15229','F1523','F1524','F15250','F15251','F15259',
'F15280','F15281','F15282','F15288','F1529','F1590','F15920','F15921','F15922','F15929','F1593','F1594','F15950','F15951','F15959','F15980',
'F15981','F15982','F15988','F1599','F1610','F16120','F16121','F16122','F16129','F1614','F16150','F16151','F16159','F16180','F16183','F16188',
'F1619','F1620','F16220','F16221','F16229','F1624','F16250','F16251','F16259','F16280','F16283','F16288','F1629','F1690','F16920','F16921',
'F16929','F1694','F16950','F16951','F16959','F16980','F16983','F16988','F1699','F17203','F17208','F17209','F17213','F17218','F17219','F17223',
'F17228','F17229','F17293','F17298','F17299','F1810','F18120','F18121','F18129','F1814','F18150','F18151','F18159','F1817','F18180','F18188',
'F1819','F1820','F18220','F18221','F18229','F1824','F18250','F18251','F18259','F1827','F18280','F18288','F1829','F1890','F18920','F18921',
'F18929','F1894','F18950','F18951','F18959','F1897','F18980','F18988','F1899','F1910','F19120','F19121','F19122','F19129','F1914','F19150',
'F19151','F19159','F1916','F1917','F19180','F19181','F19182','F19188','F1919','F1920','F19220','F19221','F19222','F19229','F19230','F19231',
'F19232','F19239','F1924','F19250','F19251','F19259','F1926','F1927','F19280','F19281','F19282','F19288','F1929','F1990','F19920','F19921',
'F19922','F19929','F19930','F19931','F19932','F19939','F1994','F19950','F19951','F19959','F1996','F1997','F19980','F19981','F19982','F19988',
'F1999','F550','F551','F552','F553','F554','F558','O355XX0','O355XX1','O355XX2','O355XX3','O355XX4','O355XX5','O355XX9','T40691A',
'T40692A','T40693A','T40694A','O99320','O99321','O99322','O99323','O99324','O99325','P0441','P0449','P961','P962','T400X1A','T400X2A',
'T400X3A','T400X4A','T400X5A','T400X5S','T401X1A','T401X2A','T401X3A','T401X4A','T402X1A','T402X2A','T402X3A','T402X4A','T403X1A','T403X2A',
'T403X3A','T403X4A','T403X5A','T403X5S','T404X1A','T404X2A','T404X3A','T404X4A','T407X1A','T408X1A','T40601A','T40602A','T40603A','T40604A',
'T40691A','T40692A','T40693A','T40694A','T40901A','T40991A','Z7141','Z7142','Z7151','Z7152','Z716')
	then drg_hh_clm=CLM_HHA_TOT_VISIT_CNT; 
/* Intellectual Disability */
	if diag{i} in ('E7871','E7872','F70','F71','F72','F73','F78','F79','P043','Q860','Q871','Q8711','Q8719','Q872','Q873',
					'Q875','Q8781','Q8789','Q897','Q898','Q900','Q901','Q902','Q909','Q910','Q911','Q912','Q913','Q914','Q915',
					'Q916','Q917','Q920','Q921','Q922','Q925','Q9261','Q9262','Q927','Q928','Q929','Q930','Q931','Q932','Q933',
					'Q934','Q935','Q9351','Q9359','Q937','Q9381','Q9388','Q9389','Q939','Q952','Q953','Q992')
	then id_hh_clm=CLM_HHA_TOT_VISIT_CNT; 
/* Autism */
	if diag{i} in ('F840','F843','F845','F848','F849')
	then au_hh_clm=CLM_HHA_TOT_VISIT_CNT; 
/* Cerebral Palsy */
	if diag{i} in ('G800','G801','G802','G803','G804','G808','G809')
	then cp_hh_clm=CLM_HHA_TOT_VISIT_CNT; 
/* ADRD */
	if diag{i} in ('F0150','F0151','F0280','F0281','F0390','F0391','F04','G138','F05','F061','F068','G300','G301','G308','G309','G311','G312',
					'G3101','G3109','G94','R4181','R54')
	then ad_hh_clm=CLM_HHA_TOT_VISIT_CNT; 
/* Depression, Bipolar, or Other Depressive Mood Disorders (CMS CCW 2022 update) */
	if diag{i} in ('F0631','F0632','F3010','F3011','F3012','F3013','F302','F303','F304','F308','F309','F310','F3110','F3111','F3112','F3113','F312',
'F3130','F3131','F3132','F314','F315','F3160','F3161','F3162','F3163','F3164','F3170','F3171','F3172','F3173','F3174','F3175',
'F3176','F3177','F3178','F3181','F3189','F319','F320','F321','F322','F323','F324','F325','F328','F3289','F329','F32A','F330',
'F331','F332','F333','F3340','F3341','F3342','F338','F339','F340','F341','F3481','F3489','F349','F39','F4321','F4323')
	then dep_hh_clm=CLM_HHA_TOT_VISIT_CNT;
/* Serious+ Persistent (Bipolar and Major) Depression ICD10 codes from Adepoju et al. JAMANO 202211 */
	if diag{i} in ('F3010','F3011','F3012','F3013','F302','F303','F304','F308','F309','F310','F3110','F3111','F3112','F3113','F312','F3130','F3131',
'F3132','F314','F315','F3160','F3161','F3162','F3163','F3164','F3170','F3171','F3172','F3173','F3174','F3175','F3176','F3177',
'F3178','F3181','F3189','F319','F320','F321','F322','F323','F324','F325','F3289','F329','F330','F331','F332','F333','F3340',
'F3341','F3342','F338','F339','F340','F341')
	then spdep_hh_clm=CLM_HHA_TOT_VISIT_CNT;
/* Anxiety Disorders */
	if diag{i} in ('F064','F4000','F4001','F4002',
				   'F4010','F4011','F40210','F40218','F40220','F40228','F40230','F40231','F40232','F40233','F40240','F40241','F40242','F40243',
				   'F40248','F40290','F40291','F40298','F408','F409','F410','F411','F413','F418','F419','F42','F422','F423','F424','F428','F429',
				   'F430','F4310','F4311','F4312','F449','F458','F488','F489','F938','F99','R452','R455','R456','R457')
	then anx_hh_clm=CLM_HHA_TOT_VISIT_CNT;
end;
run;

proc sort data = hha_&YEAR.;
by BASEID;
run;

proc summary data = hha_&YEAR.;
by BASEID;
var pvd_hh_clm alc_hh_clm drg_hh_clm id_hh_clm au_hh_clm cp_hh_clm ad_hh_clm dep_hh_clm spdep_hh_clm anx_hh_clm;
output out=hha_&YEAR. (keep=BASEID pvd_hh_clm alc_hh_clm drg_hh_clm id_hh_clm au_hh_clm cp_hh_clm ad_hh_clm dep_hh_clm spdep_hh_clm anx_hh_clm)sum=;
run;

Data snf_&YEAR. (keep = BASEID CLM_FROM_DT CLM_THRU_DT PRNCPAL_DGNS_CD ICD_DGNS_CD1 ICD_DGNS_CD2 ICD_DGNS_CD3 ICD_DGNS_CD4 ICD_DGNS_CD5 ICD_DGNS_CD6 ICD_DGNS_CD7
						ICD_DGNS_CD8 ICD_DGNS_CD9 ICD_DGNS_CD10 ICD_DGNS_CD11 ICD_DGNS_CD12 ICD_DGNS_CD13 ICD_DGNS_CD14 ICD_DGNS_CD15 ICD_DGNS_CD16 ICD_DGNS_CD17 ICD_DGNS_CD18
						ICD_DGNS_CD19 ICD_DGNS_CD20 ICD_DGNS_CD21 ICD_DGNS_CD22 ICD_DGNS_CD23 ICD_DGNS_CD24 ICD_DGNS_CD25 
						pvd_snf_clm alc_snf_clm drg_snf_clm id_snf_clm au_snf_clm cp_snf_clm ad_snf_clm dep_snf_clm spdep_snf_clm anx_snf_clm);
set claims&YR..snf;
pvd_snf_clm=0;
alc_snf_clm=0;
drg_snf_clm=0;
id_snf_clm=0;
au_snf_clm=0;
cp_snf_clm=0;
ad_snf_clm=0;
dep_snf_clm=0;
spdep_snf_clm=0;
anx_snf_clm=0;
array diag{25} ICD_DGNS_CD1-ICD_DGNS_CD25;
do i=1 to 25;

  /* peripheral vascular disease */
  if diag{i} in ('E0851','E0852','E0951','E0952','E1051','E1052','E1151','E1152','E1351','E1352','I700','I701','I70201','I70202','I70203','I70208','I70209','I70211',
'I70212','I70213','I70218','I70219','I70221','I70222','I70223','I70228','I70229','I70231','I70232','I70233','I70234','I70235','I70238','I70239','I70241','I70242','I70243',
'I70244','I70245','I70248','I70249','I7025','I70291','I70292','I70293','I70298','I70299','I7092','I7381','I7389','I739','I791','I798')
  then pvd_snf_clm=1; 
/* Alcoholism */
  if diag{i} in ('F1010','F10120','F10121','F10129','F1014','F10150','F10151','F10159','F10180','F10181','F10182','F10188','F1019','F1020','F10220','F10221','F10229','F10230','F10231',
'F10232','F10239','F1024','F10250','F10251','F10259','F1026','F1027','F10280','F10281','F10282','F10288','F1029','F10920','F10921','F10929','F1094','F10950','F10951','F10959',
'F1096','F1097','F10980','F10981','F10982','F10988','F1099','G621','I426','K2920','K2921','K700','K7010','K7011','K702','K7030','K7031','K7040','K7041','K709','P043','Q860',
'T510X1A','T510X2A','T510X3A','T510X4A','Z7141','Z7142') 														
  then alc_snf_clm=1; 
/* Drug Use Disorder */
  if diag{i} in ('F1110','F11120','F11121','F11122','F11129','F1114','F11150','F11151','F11159','F11181','F11182','F11188','F1119','F1120','F11220','F11221',
'F11222','F11229','F1123','F1124','F11250','F11251','F11259','F11281','F11282','F11288','F1129','F1190','F11920','F11921','F11922','F11929',
'F1193','F1194','F11950','F11951','F11959','F11981','F11982','F11988','F1199','F1210','F12120','F12121','F12122','F12129','F12150','F12151',
'F12159','F12180','F12188','F1219','F1220','F12220','F12221','F12222','F12229','F12250','F12251','F12259','F12280','F12288','F1229','F1290',
'F12920','F12921','F12922','F12929','F12950','F12951','F12959','F12980','F12988','F1299','F1310','F13120','F13121','F13129','F1314','F13150',
'F13151','F13159','F13180','F13181','F13182','F13188','F1319','F1320','F13220','F13221','F13229','F13230','F13231','F13232','F13239','F1324',
'F13250','F13251','F13259','F1326','F1327','F13280','F13281','F13282','F13288','F1329','F1390','F13920','F13921','F13929','F13930','F13931',
'F13932','F13939','F1394','F13950','F13951','F13959','F1396','F1397','F13980','F13981','F13982','F13988','F1399','F1410','F14120','F14121',
'F14122','F14129','F1414','F14150','F14151','F14159','F14180','F14181','F14182','F14188','F1419','F1420','F14220','F14221','F14222','F14229',
'F1423','F1424','F14250','F14251','F14259','F14280','F14281','F14282','F14288','F1429','F1490','F14920','F14921','F14922','F14929','F1494',
'F14950','F14951','F14959','F14980','F14981','F14982','F14988','F1499','F1510','F15120','F15121','F15122','F15129','F1514','F15150','F15151',
'F15159','F15180','F15181','F15182','F15188','F1519','F1520','F15220','F15221','F15222','F15229','F1523','F1524','F15250','F15251','F15259',
'F15280','F15281','F15282','F15288','F1529','F1590','F15920','F15921','F15922','F15929','F1593','F1594','F15950','F15951','F15959','F15980',
'F15981','F15982','F15988','F1599','F1610','F16120','F16121','F16122','F16129','F1614','F16150','F16151','F16159','F16180','F16183','F16188',
'F1619','F1620','F16220','F16221','F16229','F1624','F16250','F16251','F16259','F16280','F16283','F16288','F1629','F1690','F16920','F16921',
'F16929','F1694','F16950','F16951','F16959','F16980','F16983','F16988','F1699','F17203','F17208','F17209','F17213','F17218','F17219','F17223',
'F17228','F17229','F17293','F17298','F17299','F1810','F18120','F18121','F18129','F1814','F18150','F18151','F18159','F1817','F18180','F18188',
'F1819','F1820','F18220','F18221','F18229','F1824','F18250','F18251','F18259','F1827','F18280','F18288','F1829','F1890','F18920','F18921',
'F18929','F1894','F18950','F18951','F18959','F1897','F18980','F18988','F1899','F1910','F19120','F19121','F19122','F19129','F1914','F19150',
'F19151','F19159','F1916','F1917','F19180','F19181','F19182','F19188','F1919','F1920','F19220','F19221','F19222','F19229','F19230','F19231',
'F19232','F19239','F1924','F19250','F19251','F19259','F1926','F1927','F19280','F19281','F19282','F19288','F1929','F1990','F19920','F19921',
'F19922','F19929','F19930','F19931','F19932','F19939','F1994','F19950','F19951','F19959','F1996','F1997','F19980','F19981','F19982','F19988',
'F1999','F550','F551','F552','F553','F554','F558','O355XX0','O355XX1','O355XX2','O355XX3','O355XX4','O355XX5','O355XX9','T40691A',
'T40692A','T40693A','T40694A','O99320','O99321','O99322','O99323','O99324','O99325','P0441','P0449','P961','P962','T400X1A','T400X2A',
'T400X3A','T400X4A','T400X5A','T400X5S','T401X1A','T401X2A','T401X3A','T401X4A','T402X1A','T402X2A','T402X3A','T402X4A','T403X1A','T403X2A',
'T403X3A','T403X4A','T403X5A','T403X5S','T404X1A','T404X2A','T404X3A','T404X4A','T407X1A','T408X1A','T40601A','T40602A','T40603A','T40604A',
'T40691A','T40692A','T40693A','T40694A','T40901A','T40991A','Z7141','Z7142','Z7151','Z7152','Z716')
	then drg_snf_clm=1; 
/* Intellectual Disability */
	if diag{i} in ('E7871','E7872','F70','F71','F72','F73','F78','F79','P043','Q860','Q871','Q8711','Q8719','Q872','Q873',
					'Q875','Q8781','Q8789','Q897','Q898','Q900','Q901','Q902','Q909','Q910','Q911','Q912','Q913','Q914','Q915',
					'Q916','Q917','Q920','Q921','Q922','Q925','Q9261','Q9262','Q927','Q928','Q929','Q930','Q931','Q932','Q933',
					'Q934','Q935','Q9351','Q9359','Q937','Q9381','Q9388','Q9389','Q939','Q952','Q953','Q992')
	then id_snf_clm=1; 
/* Autism */
	if diag{i} in ('F840','F843','F845','F848','F849')
	then au_snf_clm=1; 
/* Cerebral Palsy */
	if diag{i} in ('G800','G801','G802','G803','G804','G808','G809')
	then cp_snf_clm=1; 
/* ADRD */
	if diag{i} in ('F0150','F0151','F0280','F0281','F0390','F0391','F04','G138','F05','F061','F068','G300','G301','G308','G309','G311','G312',
					'G3101','G3109','G94','R4181','R54')
	then ad_snf_clm=1; 
/* Depression */
	if diag{i} in ('F0631','F0632','F3010','F3011','F3012','F3013','F302','F303','F304','F308','F309','F310','F3110','F3111','F3112','F3113','F312',
'F3130','F3131','F3132','F314','F315','F3160','F3161','F3162','F3163','F3164','F3170','F3171','F3172','F3173','F3174','F3175',
'F3176','F3177','F3178','F3181','F3189','F319','F320','F321','F322','F323','F324','F325','F328','F3289','F329','F32A','F330',
'F331','F332','F333','F3340','F3341','F3342','F338','F339','F340','F341','F3481','F3489','F349','F39','F4321','F4323')
	then dep_snf_clm=1;
/* Serious+ Persistent (Bipolar and Major) Depression ICD10 codes from Adepoju et al. JAMANO 202211 */
	if diag{i} in ('F3010','F3011','F3012','F3013','F302','F303','F304','F308','F309','F310','F3110','F3111','F3112','F3113','F312','F3130','F3131',
'F3132','F314','F315','F3160','F3161','F3162','F3163','F3164','F3170','F3171','F3172','F3173','F3174','F3175','F3176','F3177',
'F3178','F3181','F3189','F319','F320','F321','F322','F323','F324','F325','F3289','F329','F330','F331','F332','F333','F3340',
'F3341','F3342','F338','F339','F340','F341')
	then spdep_snf_clm=1;
/* Anxiety Disorders */
	if diag{i} in ('F064','F4000','F4001','F4002',
				   'F4010','F4011','F40210','F40218','F40220','F40228','F40230','F40231','F40232','F40233','F40240','F40241','F40242','F40243',
				   'F40248','F40290','F40291','F40298','F408','F409','F410','F411','F413','F418','F419','F42','F422','F423','F424','F428','F429',
				   'F430','F4310','F4311','F4312','F449','F458','F488','F489','F938','F99','R452','R455','R456','R457')
	then anx_snf_clm=1;
end;
/* Alcoholism */
array proc{25} ICD_PRCDR_CD1-ICD_PRCDR_CD25;
do i=1 to 25;
if proc{i} in ('HZ2ZZZZ','HZ30ZZZ','HZ31ZZZ','HZ32ZZZ','HZ33ZZZ','HZ34ZZZ','HZ35ZZZ','HZ36ZZZ','HZ37ZZZ','HZ38ZZZ','HZ39ZZZ','HZ3BZZZ','HZ40ZZZ','HZ93ZZZ','HZ96ZZZ')
then alc_snf_clm=1; 
/* Drug Use Disorder */
if proc{i} in ('HZ2ZZZZ','HZ30ZZZ','HZ31ZZZ','HZ32ZZZ','HZ33ZZZ','HZ34ZZZ','HZ35ZZZ','HZ36ZZZ','HZ37ZZZ','HZ38ZZZ','HZ39ZZZ','HZ3BZZZ','HZ40ZZZ','HZ93ZZZ','HZ96ZZZ')
then drg_snf_clm=1;
end;

run;

proc sort data = snf_&YEAR.;
by BASEID;
run;

proc summary data = snf_&YEAR.;
by BASEID;
var pvd_snf_clm alc_snf_clm drg_snf_clm id_snf_clm au_snf_clm cp_snf_clm ad_snf_clm dep_snf_clm spdep_snf_clm anx_snf_clm;
output out=snf_&YEAR. (keep=BASEID pvd_snf_clm alc_snf_clm drg_snf_clm id_snf_clm au_snf_clm cp_snf_clm ad_snf_clm dep_snf_clm spdep_snf_clm anx_snf_clm)sum=;
run;

Data inp_&YEAR. (keep = BASEID CLM_FROM_DT CLM_THRU_DT PRNCPAL_DGNS_CD ICD_DGNS_CD1 ICD_DGNS_CD2 ICD_DGNS_CD3 ICD_DGNS_CD4 ICD_DGNS_CD5 ICD_DGNS_CD6 ICD_DGNS_CD7 ICD_DGNS_CD8 
					    ICD_DGNS_CD9 ICD_DGNS_CD10 ICD_DGNS_CD11 ICD_DGNS_CD12 ICD_DGNS_CD13 ICD_DGNS_CD14 ICD_DGNS_CD15 ICD_DGNS_CD16 ICD_DGNS_CD17 ICD_DGNS_CD18 ICD_DGNS_CD19 
					    ICD_DGNS_CD20 ICD_DGNS_CD21 ICD_DGNS_CD22 ICD_DGNS_CD23 ICD_DGNS_CD24 ICD_DGNS_CD25 
						pvd_inp_clm alc_inp_clm drg_inp_clm id_inp_clm au_inp_clm cp_inp_clm ad_inp_clm dep_inp_clm spdep_inp_clm anx_inp_clm);
set claims&YR..inp;
pvd_inp_clm=0;
alc_inp_clm=0;
drg_inp_clm=0;
id_inp_clm=0;
au_inp_clm=0; 
cp_inp_clm=0;
ad_inp_clm=0;
dep_inp_clm=0;
spdep_inp_clm=0;
anx_inp_clm=0;
array diag{25} ICD_DGNS_CD1-ICD_DGNS_CD25;
do i=1 to 25;

/* peripheral vascular disease */
  if diag{i} in ('E0851','E0852','E0951','E0952','E1051','E1052','E1151','E1152','E1351','E1352','I700','I701','I70201','I70202','I70203','I70208','I70209','I70211',
'I70212','I70213','I70218','I70219','I70221','I70222','I70223','I70228','I70229','I70231','I70232','I70233','I70234','I70235','I70238','I70239','I70241','I70242','I70243',
'I70244','I70245','I70248','I70249','I7025','I70291','I70292','I70293','I70298','I70299','I7092','I7381','I7389','I739','I791','I798')
  then pvd_inp_clm=1; 
/* Alcoholism */
  if diag{i} in ('F1010','F10120','F10121','F10129','F1014','F10150','F10151','F10159','F10180','F10181','F10182','F10188','F1019','F1020','F10220','F10221','F10229','F10230','F10231',
'F10232','F10239','F1024','F10250','F10251','F10259','F1026','F1027','F10280','F10281','F10282','F10288','F1029','F10920','F10921','F10929','F1094','F10950','F10951','F10959',
'F1096','F1097','F10980','F10981','F10982','F10988','F1099','G621','I426','K2920','K2921','K700','K7010','K7011','K702','K7030','K7031','K7040','K7041','K709','P043','Q860',
'T510X1A','T510X2A','T510X3A','T510X4A','Z7141','Z7142') 														
  then alc_inp_clm=1; 
/* Drug Use Disorder */
  if diag{i} in ('F1110','F11120','F11121','F11122','F11129','F1114','F11150','F11151','F11159','F11181','F11182','F11188','F1119','F1120','F11220','F11221',
'F11222','F11229','F1123','F1124','F11250','F11251','F11259','F11281','F11282','F11288','F1129','F1190','F11920','F11921','F11922','F11929',
'F1193','F1194','F11950','F11951','F11959','F11981','F11982','F11988','F1199','F1210','F12120','F12121','F12122','F12129','F12150','F12151',
'F12159','F12180','F12188','F1219','F1220','F12220','F12221','F12222','F12229','F12250','F12251','F12259','F12280','F12288','F1229','F1290',
'F12920','F12921','F12922','F12929','F12950','F12951','F12959','F12980','F12988','F1299','F1310','F13120','F13121','F13129','F1314','F13150',
'F13151','F13159','F13180','F13181','F13182','F13188','F1319','F1320','F13220','F13221','F13229','F13230','F13231','F13232','F13239','F1324',
'F13250','F13251','F13259','F1326','F1327','F13280','F13281','F13282','F13288','F1329','F1390','F13920','F13921','F13929','F13930','F13931',
'F13932','F13939','F1394','F13950','F13951','F13959','F1396','F1397','F13980','F13981','F13982','F13988','F1399','F1410','F14120','F14121',
'F14122','F14129','F1414','F14150','F14151','F14159','F14180','F14181','F14182','F14188','F1419','F1420','F14220','F14221','F14222','F14229',
'F1423','F1424','F14250','F14251','F14259','F14280','F14281','F14282','F14288','F1429','F1490','F14920','F14921','F14922','F14929','F1494',
'F14950','F14951','F14959','F14980','F14981','F14982','F14988','F1499','F1510','F15120','F15121','F15122','F15129','F1514','F15150','F15151',
'F15159','F15180','F15181','F15182','F15188','F1519','F1520','F15220','F15221','F15222','F15229','F1523','F1524','F15250','F15251','F15259',
'F15280','F15281','F15282','F15288','F1529','F1590','F15920','F15921','F15922','F15929','F1593','F1594','F15950','F15951','F15959','F15980',
'F15981','F15982','F15988','F1599','F1610','F16120','F16121','F16122','F16129','F1614','F16150','F16151','F16159','F16180','F16183','F16188',
'F1619','F1620','F16220','F16221','F16229','F1624','F16250','F16251','F16259','F16280','F16283','F16288','F1629','F1690','F16920','F16921',
'F16929','F1694','F16950','F16951','F16959','F16980','F16983','F16988','F1699','F17203','F17208','F17209','F17213','F17218','F17219','F17223',
'F17228','F17229','F17293','F17298','F17299','F1810','F18120','F18121','F18129','F1814','F18150','F18151','F18159','F1817','F18180','F18188',
'F1819','F1820','F18220','F18221','F18229','F1824','F18250','F18251','F18259','F1827','F18280','F18288','F1829','F1890','F18920','F18921',
'F18929','F1894','F18950','F18951','F18959','F1897','F18980','F18988','F1899','F1910','F19120','F19121','F19122','F19129','F1914','F19150',
'F19151','F19159','F1916','F1917','F19180','F19181','F19182','F19188','F1919','F1920','F19220','F19221','F19222','F19229','F19230','F19231',
'F19232','F19239','F1924','F19250','F19251','F19259','F1926','F1927','F19280','F19281','F19282','F19288','F1929','F1990','F19920','F19921',
'F19922','F19929','F19930','F19931','F19932','F19939','F1994','F19950','F19951','F19959','F1996','F1997','F19980','F19981','F19982','F19988',
'F1999','F550','F551','F552','F553','F554','F558','O355XX0','O355XX1','O355XX2','O355XX3','O355XX4','O355XX5','O355XX9','T40691A',
'T40692A','T40693A','T40694A','O99320','O99321','O99322','O99323','O99324','O99325','P0441','P0449','P961','P962','T400X1A','T400X2A',
'T400X3A','T400X4A','T400X5A','T400X5S','T401X1A','T401X2A','T401X3A','T401X4A','T402X1A','T402X2A','T402X3A','T402X4A','T403X1A','T403X2A',
'T403X3A','T403X4A','T403X5A','T403X5S','T404X1A','T404X2A','T404X3A','T404X4A','T407X1A','T408X1A','T40601A','T40602A','T40603A','T40604A',
'T40691A','T40692A','T40693A','T40694A','T40901A','T40991A','Z7141','Z7142','Z7151','Z7152','Z716')
	then drg_inp_clm=1;
/* Intellectual Disability */
	if diag{i} in ('E7871','E7872','F70','F71','F72','F73','F78','F79','P043','Q860','Q871','Q8711','Q8719','Q872','Q873',
					'Q875','Q8781','Q8789','Q897','Q898','Q900','Q901','Q902','Q909','Q910','Q911','Q912','Q913','Q914','Q915',
					'Q916','Q917','Q920','Q921','Q922','Q925','Q9261','Q9262','Q927','Q928','Q929','Q930','Q931','Q932','Q933',
					'Q934','Q935','Q9351','Q9359','Q937','Q9381','Q9388','Q9389','Q939','Q952','Q953','Q992')
	then id_inp_clm=1; 
/* Autism */
	if diag{i} in ('F840','F843','F845','F848','F849')
	then au_inp_clm=1; 
/* Cerebral Palsy */
	if diag{i} in ('G800','G801','G802','G803','G804','G808','G809')
	then cp_inp_clm=1;  
/* ADRD */
	if diag{i} in ('F0150','F0151','F0280','F0281','F0390','F0391','F04','G138','F05','F061','F068','G300','G301','G308','G309','G311','G312',
					'G3101','G3109','G94','R4181','R54')
	then ad_inp_clm=1; 
/* Depression */
	if diag{i} in ('F0631','F0632','F3010','F3011','F3012','F3013','F302','F303','F304','F308','F309','F310','F3110','F3111','F3112','F3113','F312',
'F3130','F3131','F3132','F314','F315','F3160','F3161','F3162','F3163','F3164','F3170','F3171','F3172','F3173','F3174','F3175',
'F3176','F3177','F3178','F3181','F3189','F319','F320','F321','F322','F323','F324','F325','F328','F3289','F329','F32A','F330',
'F331','F332','F333','F3340','F3341','F3342','F338','F339','F340','F341','F3481','F3489','F349','F39','F4321','F4323')
	then dep_inp_clm=1;
/* Serious+ Persistent (Bipolar and Major) Depression ICD10 codes from Adepoju et al. JAMANO 202211 */
	if diag{i} in ('F3010','F3011','F3012','F3013','F302','F303','F304','F308','F309','F310','F3110','F3111','F3112','F3113','F312','F3130','F3131',
'F3132','F314','F315','F3160','F3161','F3162','F3163','F3164','F3170','F3171','F3172','F3173','F3174','F3175','F3176','F3177',
'F3178','F3181','F3189','F319','F320','F321','F322','F323','F324','F325','F3289','F329','F330','F331','F332','F333','F3340',
'F3341','F3342','F338','F339','F340','F341')
	then spdep_inp_clm=1;
/* Anxiety Disorders */
	if diag{i} in ('F064','F4000','F4001','F4002',
				   'F4010','F4011','F40210','F40218','F40220','F40228','F40230','F40231','F40232','F40233','F40240','F40241','F40242','F40243',
				   'F40248','F40290','F40291','F40298','F408','F409','F410','F411','F413','F418','F419','F42','F422','F423','F424','F428','F429',
				   'F430','F4310','F4311','F4312','F449','F458','F488','F489','F938','F99','R452','R455','R456','R457')
	then anx_inp_clm=1;
end;
/* Alcoholism */
array proc{25} ICD_PRCDR_CD1-ICD_PRCDR_CD25;
do i=1 to 25;
if proc{i} in ('HZ2ZZZZ','HZ30ZZZ','HZ31ZZZ','HZ32ZZZ','HZ33ZZZ','HZ34ZZZ','HZ35ZZZ','HZ36ZZZ','HZ37ZZZ','HZ38ZZZ','HZ39ZZZ','HZ3BZZZ','HZ40ZZZ','HZ93ZZZ','HZ96ZZZ')
then alc_inp_clm=1; 
/* Drug Use Disorder */
if proc{i} in ('HZ2ZZZZ','HZ30ZZZ','HZ31ZZZ','HZ32ZZZ','HZ33ZZZ','HZ34ZZZ','HZ35ZZZ','HZ36ZZZ','HZ37ZZZ','HZ38ZZZ','HZ39ZZZ','HZ3BZZZ','HZ40ZZZ','HZ93ZZZ','HZ96ZZZ')
then drg_inp_clm=1;
end;

run;

proc sort data = inp_&YEAR.;
by BASEID;
run;

proc summary data = inp_&YEAR.;
by BASEID;
var pvd_inp_clm alc_inp_clm drg_inp_clm id_inp_clm au_inp_clm cp_inp_clm ad_inp_clm dep_inp_clm spdep_inp_clm anx_inp_clm;
output out=inp_&YEAR. (keep=BASEID pvd_inp_clm alc_inp_clm drg_inp_clm id_inp_clm au_inp_clm cp_inp_clm ad_inp_clm dep_inp_clm spdep_inp_clm anx_inp_clm)sum=;
run;

Data otp_&YEAR. (keep = BASEID CLAIMID CLM_FROM_DT CLM_THRU_DT PRNCPAL_DGNS_CD ICD_DGNS_CD1 ICD_DGNS_CD2 ICD_DGNS_CD3 ICD_DGNS_CD4 ICD_DGNS_CD5 ICD_DGNS_CD6 ICD_DGNS_CD7 ICD_DGNS_CD8 
					    ICD_DGNS_CD9 ICD_DGNS_CD10 ICD_DGNS_CD11 ICD_DGNS_CD12 ICD_DGNS_CD13 ICD_DGNS_CD14 ICD_DGNS_CD15 ICD_DGNS_CD16 ICD_DGNS_CD17 ICD_DGNS_CD18 ICD_DGNS_CD19 
					    ICD_DGNS_CD20 ICD_DGNS_CD21 ICD_DGNS_CD22 ICD_DGNS_CD23 ICD_DGNS_CD24 ICD_DGNS_CD25 
						pvd_otp_clm alc_otp_clm drg_otp_clm id_otp_clm au_otp_clm cp_otp_clm ad_otp_clm dep_otp_clm spdep_otp_clm anx_otp_clm);
set claims&YR..otp;
pvd_otp_clm=0;
alc_otp_clm=0;
drg_otp_clm=0;
id_otp_clm=0;
au_otp_clm=0;
cp_otp_clm=0;
ad_otp_clm=0;
dep_otp_clm=0;
spdep_otp_clm=0;
anx_otp_clm=0;
array diag{25} ICD_DGNS_CD1-ICD_DGNS_CD25;
do i=1 to 25;

/* peripheral vascular disease */
  if diag{i} in ('E0851','E0852','E0951','E0952','E1051','E1052','E1151','E1152','E1351','E1352','I700','I701','I70201','I70202','I70203','I70208','I70209','I70211',
'I70212','I70213','I70218','I70219','I70221','I70222','I70223','I70228','I70229','I70231','I70232','I70233','I70234','I70235','I70238','I70239','I70241','I70242','I70243',
'I70244','I70245','I70248','I70249','I7025','I70291','I70292','I70293','I70298','I70299','I7092','I7381','I7389','I739','I791','I798')
  then pvd_otp_clm=1; 
/* Alcoholism */
  if diag{i} in ('F1010','F10120','F10121','F10129','F1014','F10150','F10151','F10159','F10180','F10181','F10182','F10188','F1019','F1020','F10220','F10221','F10229','F10230','F10231',
'F10232','F10239','F1024','F10250','F10251','F10259','F1026','F1027','F10280','F10281','F10282','F10288','F1029','F10920','F10921','F10929','F1094','F10950','F10951','F10959',
'F1096','F1097','F10980','F10981','F10982','F10988','F1099','G621','I426','K2920','K2921','K700','K7010','K7011','K702','K7030','K7031','K7040','K7041','K709','P043','Q860',
'T510X1A','T510X2A','T510X3A','T510X4A','Z7141','Z7142') 														
  then alc_otp_clm=1; 
/* Drug Use Disorder */
  if diag{i} in ('F1110','F11120','F11121','F11122','F11129','F1114','F11150','F11151','F11159','F11181','F11182','F11188','F1119','F1120','F11220','F11221',
'F11222','F11229','F1123','F1124','F11250','F11251','F11259','F11281','F11282','F11288','F1129','F1190','F11920','F11921','F11922','F11929',
'F1193','F1194','F11950','F11951','F11959','F11981','F11982','F11988','F1199','F1210','F12120','F12121','F12122','F12129','F12150','F12151',
'F12159','F12180','F12188','F1219','F1220','F12220','F12221','F12222','F12229','F12250','F12251','F12259','F12280','F12288','F1229','F1290',
'F12920','F12921','F12922','F12929','F12950','F12951','F12959','F12980','F12988','F1299','F1310','F13120','F13121','F13129','F1314','F13150',
'F13151','F13159','F13180','F13181','F13182','F13188','F1319','F1320','F13220','F13221','F13229','F13230','F13231','F13232','F13239','F1324',
'F13250','F13251','F13259','F1326','F1327','F13280','F13281','F13282','F13288','F1329','F1390','F13920','F13921','F13929','F13930','F13931',
'F13932','F13939','F1394','F13950','F13951','F13959','F1396','F1397','F13980','F13981','F13982','F13988','F1399','F1410','F14120','F14121',
'F14122','F14129','F1414','F14150','F14151','F14159','F14180','F14181','F14182','F14188','F1419','F1420','F14220','F14221','F14222','F14229',
'F1423','F1424','F14250','F14251','F14259','F14280','F14281','F14282','F14288','F1429','F1490','F14920','F14921','F14922','F14929','F1494',
'F14950','F14951','F14959','F14980','F14981','F14982','F14988','F1499','F1510','F15120','F15121','F15122','F15129','F1514','F15150','F15151',
'F15159','F15180','F15181','F15182','F15188','F1519','F1520','F15220','F15221','F15222','F15229','F1523','F1524','F15250','F15251','F15259',
'F15280','F15281','F15282','F15288','F1529','F1590','F15920','F15921','F15922','F15929','F1593','F1594','F15950','F15951','F15959','F15980',
'F15981','F15982','F15988','F1599','F1610','F16120','F16121','F16122','F16129','F1614','F16150','F16151','F16159','F16180','F16183','F16188',
'F1619','F1620','F16220','F16221','F16229','F1624','F16250','F16251','F16259','F16280','F16283','F16288','F1629','F1690','F16920','F16921',
'F16929','F1694','F16950','F16951','F16959','F16980','F16983','F16988','F1699','F17203','F17208','F17209','F17213','F17218','F17219','F17223',
'F17228','F17229','F17293','F17298','F17299','F1810','F18120','F18121','F18129','F1814','F18150','F18151','F18159','F1817','F18180','F18188',
'F1819','F1820','F18220','F18221','F18229','F1824','F18250','F18251','F18259','F1827','F18280','F18288','F1829','F1890','F18920','F18921',
'F18929','F1894','F18950','F18951','F18959','F1897','F18980','F18988','F1899','F1910','F19120','F19121','F19122','F19129','F1914','F19150',
'F19151','F19159','F1916','F1917','F19180','F19181','F19182','F19188','F1919','F1920','F19220','F19221','F19222','F19229','F19230','F19231',
'F19232','F19239','F1924','F19250','F19251','F19259','F1926','F1927','F19280','F19281','F19282','F19288','F1929','F1990','F19920','F19921',
'F19922','F19929','F19930','F19931','F19932','F19939','F1994','F19950','F19951','F19959','F1996','F1997','F19980','F19981','F19982','F19988',
'F1999','F550','F551','F552','F553','F554','F558','O355XX0','O355XX1','O355XX2','O355XX3','O355XX4','O355XX5','O355XX9','T40691A',
'T40692A','T40693A','T40694A','O99320','O99321','O99322','O99323','O99324','O99325','P0441','P0449','P961','P962','T400X1A','T400X2A',
'T400X3A','T400X4A','T400X5A','T400X5S','T401X1A','T401X2A','T401X3A','T401X4A','T402X1A','T402X2A','T402X3A','T402X4A','T403X1A','T403X2A',
'T403X3A','T403X4A','T403X5A','T403X5S','T404X1A','T404X2A','T404X3A','T404X4A','T407X1A','T408X1A','T40601A','T40602A','T40603A','T40604A',
'T40691A','T40692A','T40693A','T40694A','T40901A','T40991A','Z7141','Z7142','Z7151','Z7152','Z716')
	then drg_otp_clm=1; 
/* Intellectual Disability */
	if diag{i} in ('E7871','E7872','F70','F71','F72','F73','F78','F79','P043','Q860','Q871','Q8711','Q8719','Q872','Q873',
					'Q875','Q8781','Q8789','Q897','Q898','Q900','Q901','Q902','Q909','Q910','Q911','Q912','Q913','Q914','Q915',
					'Q916','Q917','Q920','Q921','Q922','Q925','Q9261','Q9262','Q927','Q928','Q929','Q930','Q931','Q932','Q933',
					'Q934','Q935','Q9351','Q9359','Q937','Q9381','Q9388','Q9389','Q939','Q952','Q953','Q992')
	then id_otp_clm=1; 
/* Autism */
	if diag{i} in ('F840','F843','F845','F848','F849')
	then au_otp_clm=1; 
/* Cerebral Palsy */
	if diag{i} in ('G800','G801','G802','G803','G804','G808','G809')
	then cp_otp_clm=1;  
/* ADRD */
	if diag{i} in ('F0150','F0151','F0280','F0281','F0390','F0391','F04','G138','F05','F061','F068','G300','G301','G308','G309','G311','G312',
					'G3101','G3109','G94','R4181','R54')
	then ad_otp_clm=1; 
/* Depression */
	if diag{i} in ('F0631','F0632','F3010','F3011','F3012','F3013','F302','F303','F304','F308','F309','F310','F3110','F3111','F3112','F3113','F312',
'F3130','F3131','F3132','F314','F315','F3160','F3161','F3162','F3163','F3164','F3170','F3171','F3172','F3173','F3174','F3175',
'F3176','F3177','F3178','F3181','F3189','F319','F320','F321','F322','F323','F324','F325','F328','F3289','F329','F32A','F330',
'F331','F332','F333','F3340','F3341','F3342','F338','F339','F340','F341','F3481','F3489','F349','F39','F4321','F4323')
	then dep_otp_clm=1;
/* Serious+ Persistent (Bipolar and Major) Depression ICD10 codes from Adepoju et al. JAMANO 202211 */
	if diag{i} in ('F3010','F3011','F3012','F3013','F302','F303','F304','F308','F309','F310','F3110','F3111','F3112','F3113','F312','F3130','F3131',
'F3132','F314','F315','F3160','F3161','F3162','F3163','F3164','F3170','F3171','F3172','F3173','F3174','F3175','F3176','F3177',
'F3178','F3181','F3189','F319','F320','F321','F322','F323','F324','F325','F3289','F329','F330','F331','F332','F333','F3340',
'F3341','F3342','F338','F339','F340','F341')
	then spdep_otp_clm=1;
/* Anxiety Disorders */
	if diag{i} in ('F064','F4000','F4001','F4002',
				   'F4010','F4011','F40210','F40218','F40220','F40228','F40230','F40231','F40232','F40233','F40240','F40241','F40242','F40243',
				   'F40248','F40290','F40291','F40298','F408','F409','F410','F411','F413','F418','F419','F42','F422','F423','F424','F428','F429',
				   'F430','F4310','F4311','F4312','F449','F458','F488','F489','F938','F99','R452','R455','R456','R457')
	then anx_otp_clm=1;
end;
/* Alcoholism */
array proc{25} ICD_PRCDR_CD1-ICD_PRCDR_CD25;
do i=1 to 25;
if proc{i} in ('HZ2ZZZZ','HZ30ZZZ','HZ31ZZZ','HZ32ZZZ','HZ33ZZZ','HZ34ZZZ','HZ35ZZZ','HZ36ZZZ','HZ37ZZZ','HZ38ZZZ','HZ39ZZZ','HZ3BZZZ','HZ40ZZZ','HZ93ZZZ','HZ96ZZZ')
then alc_otp_clm=1; 
/* Drug Use Disorder */
if proc{i} in ('HZ2ZZZZ','HZ30ZZZ','HZ31ZZZ','HZ32ZZZ','HZ33ZZZ','HZ34ZZZ','HZ35ZZZ','HZ36ZZZ','HZ37ZZZ','HZ38ZZZ','HZ39ZZZ','HZ3BZZZ','HZ40ZZZ','HZ93ZZZ','HZ96ZZZ')
then drg_otp_clm=1;
end;
run;

proc sort data = otp_&YEAR.;
by BASEID;
run;

proc summary data = otp_&YEAR.;
by BASEID;
var pvd_otp_clm alc_otp_clm drg_otp_clm id_otp_clm au_otp_clm cp_otp_clm ad_otp_clm dep_otp_clm spdep_otp_clm anx_otp_clm;
output out=otp_&YEAR. (keep=BASEID pvd_otp_clm alc_otp_clm drg_otp_clm id_otp_clm au_otp_clm cp_otp_clm ad_otp_clm dep_otp_clm spdep_otp_clm anx_otp_clm)sum=;
run;

Data phy_&YEAR. (keep = BASEID ClaimID CLM_THRU_DT ICD_DGNS_CD1 ICD_DGNS_CD2 ICD_DGNS_CD3 ICD_DGNS_CD4 ICD_DGNS_CD5 ICD_DGNS_CD6 ICD_DGNS_CD7 ICD_DGNS_CD8 ICD_DGNS_CD9 ICD_DGNS_CD10
				        ICD_DGNS_CD11 ICD_DGNS_CD12 pvd_phy_clm alc_phy_clm drg_phy_clm id_phy_clm au_phy_clm cp_phy_clm 
						ad_phy_clm dep_phy_clm spdep_phy_clm anx_phy_clm);
set claims&YR..phy;
if NCH_CLM_TYPE_CD in ('71','72'); /* restricting to licensed healthcare professional visit per CCW algorithm */
if BETOS_CD not in ('D1A','D1B','D1C','D1D','D1E','D1F','D1G','O1A'); /* as per CMS CCW algorithm, exclude claims for DME or ambulance services */
pvd_phy_clm=0;
alc_phy_clm=0;
drg_phy_clm=0;
id_phy_clm=0;
au_phy_clm=0;
cp_phy_clm=0;
ad_phy_clm=0;
dep_phy_clm=0;
spdep_phy_clm=0;
anx_phy_clm=0;
array diag{12} ICD_DGNS_CD1-ICD_DGNS_CD12;
do i=1 to 12;

/* peripheral vascular disease */
  if diag{i} in ('E0851','E0852','E0951','E0952','E1051','E1052','E1151','E1152','E1351','E1352','I700','I701','I70201','I70202','I70203','I70208','I70209','I70211',
'I70212','I70213','I70218','I70219','I70221','I70222','I70223','I70228','I70229','I70231','I70232','I70233','I70234','I70235','I70238','I70239','I70241','I70242','I70243',
'I70244','I70245','I70248','I70249','I7025','I70291','I70292','I70293','I70298','I70299','I7092','I7381','I7389','I739','I791','I798')
  then pvd_phy_clm=1; 
/* Alcoholism */
  if diag{i} in ('F1010','F10120','F10121','F10129','F1014','F10150','F10151','F10159','F10180','F10181','F10182','F10188','F1019','F1020','F10220','F10221','F10229','F10230','F10231',
'F10232','F10239','F1024','F10250','F10251','F10259','F1026','F1027','F10280','F10281','F10282','F10288','F1029','F10920','F10921','F10929','F1094','F10950','F10951','F10959',
'F1096','F1097','F10980','F10981','F10982','F10988','F1099','G621','I426','K2920','K2921','K700','K7010','K7011','K702','K7030','K7031','K7040','K7041','K709','P043','Q860',
'T510X1A','T510X2A','T510X3A','T510X4A','Z7141','Z7142') 														
  then alc_phy_clm=1; 
/* Drug Use Disorder */
  if diag{i} in ('F1110','F11120','F11121','F11122','F11129','F1114','F11150','F11151','F11159','F11181','F11182','F11188','F1119','F1120','F11220','F11221',
'F11222','F11229','F1123','F1124','F11250','F11251','F11259','F11281','F11282','F11288','F1129','F1190','F11920','F11921','F11922','F11929',
'F1193','F1194','F11950','F11951','F11959','F11981','F11982','F11988','F1199','F1210','F12120','F12121','F12122','F12129','F12150','F12151',
'F12159','F12180','F12188','F1219','F1220','F12220','F12221','F12222','F12229','F12250','F12251','F12259','F12280','F12288','F1229','F1290',
'F12920','F12921','F12922','F12929','F12950','F12951','F12959','F12980','F12988','F1299','F1310','F13120','F13121','F13129','F1314','F13150',
'F13151','F13159','F13180','F13181','F13182','F13188','F1319','F1320','F13220','F13221','F13229','F13230','F13231','F13232','F13239','F1324',
'F13250','F13251','F13259','F1326','F1327','F13280','F13281','F13282','F13288','F1329','F1390','F13920','F13921','F13929','F13930','F13931',
'F13932','F13939','F1394','F13950','F13951','F13959','F1396','F1397','F13980','F13981','F13982','F13988','F1399','F1410','F14120','F14121',
'F14122','F14129','F1414','F14150','F14151','F14159','F14180','F14181','F14182','F14188','F1419','F1420','F14220','F14221','F14222','F14229',
'F1423','F1424','F14250','F14251','F14259','F14280','F14281','F14282','F14288','F1429','F1490','F14920','F14921','F14922','F14929','F1494',
'F14950','F14951','F14959','F14980','F14981','F14982','F14988','F1499','F1510','F15120','F15121','F15122','F15129','F1514','F15150','F15151',
'F15159','F15180','F15181','F15182','F15188','F1519','F1520','F15220','F15221','F15222','F15229','F1523','F1524','F15250','F15251','F15259',
'F15280','F15281','F15282','F15288','F1529','F1590','F15920','F15921','F15922','F15929','F1593','F1594','F15950','F15951','F15959','F15980',
'F15981','F15982','F15988','F1599','F1610','F16120','F16121','F16122','F16129','F1614','F16150','F16151','F16159','F16180','F16183','F16188',
'F1619','F1620','F16220','F16221','F16229','F1624','F16250','F16251','F16259','F16280','F16283','F16288','F1629','F1690','F16920','F16921',
'F16929','F1694','F16950','F16951','F16959','F16980','F16983','F16988','F1699','F17203','F17208','F17209','F17213','F17218','F17219','F17223',
'F17228','F17229','F17293','F17298','F17299','F1810','F18120','F18121','F18129','F1814','F18150','F18151','F18159','F1817','F18180','F18188',
'F1819','F1820','F18220','F18221','F18229','F1824','F18250','F18251','F18259','F1827','F18280','F18288','F1829','F1890','F18920','F18921',
'F18929','F1894','F18950','F18951','F18959','F1897','F18980','F18988','F1899','F1910','F19120','F19121','F19122','F19129','F1914','F19150',
'F19151','F19159','F1916','F1917','F19180','F19181','F19182','F19188','F1919','F1920','F19220','F19221','F19222','F19229','F19230','F19231',
'F19232','F19239','F1924','F19250','F19251','F19259','F1926','F1927','F19280','F19281','F19282','F19288','F1929','F1990','F19920','F19921',
'F19922','F19929','F19930','F19931','F19932','F19939','F1994','F19950','F19951','F19959','F1996','F1997','F19980','F19981','F19982','F19988',
'F1999','F550','F551','F552','F553','F554','F558','O355XX0','O355XX1','O355XX2','O355XX3','O355XX4','O355XX5','O355XX9','T40691A',
'T40692A','T40693A','T40694A','O99320','O99321','O99322','O99323','O99324','O99325','P0441','P0449','P961','P962','T400X1A','T400X2A',
'T400X3A','T400X4A','T400X5A','T400X5S','T401X1A','T401X2A','T401X3A','T401X4A','T402X1A','T402X2A','T402X3A','T402X4A','T403X1A','T403X2A',
'T403X3A','T403X4A','T403X5A','T403X5S','T404X1A','T404X2A','T404X3A','T404X4A','T407X1A','T408X1A','T40601A','T40602A','T40603A','T40604A',
'T40691A','T40692A','T40693A','T40694A','T40901A','T40991A','Z7141','Z7142','Z7151','Z7152','Z716')
	then drg_phy_clm=1; 
/* Intellectual Disability */
	if diag{i} in ('E7871','E7872','F70','F71','F72','F73','F78','F79','P043','Q860','Q871','Q8711','Q8719','Q872','Q873',
					'Q875','Q8781','Q8789','Q897','Q898','Q900','Q901','Q902','Q909','Q910','Q911','Q912','Q913','Q914','Q915',
					'Q916','Q917','Q920','Q921','Q922','Q925','Q9261','Q9262','Q927','Q928','Q929','Q930','Q931','Q932','Q933',
					'Q934','Q935','Q9351','Q9359','Q937','Q9381','Q9388','Q9389','Q939','Q952','Q953','Q992')
	then id_phy_clm=1; 
/* Autism */
	if diag{i} in ('F840','F843','F845','F848','F849')
	then au_phy_clm=1; 
/* Cerebral Palsy */
	if diag{i} in ('G800','G801','G802','G803','G804','G808','G809')
	then cp_phy_clm=1;  
/* ADRD */
	if diag{i} in ('F0150','F0151','F0280','F0281','F0390','F0391','F04','G138','F05','F061','F068','G300','G301','G308','G309','G311','G312',
					'G3101','G3109','G94','R4181','R54')
	then ad_phy_clm=1; 
/* Depression */
	if diag{i} in ('F0631','F0632','F3010','F3011','F3012','F3013','F302','F303','F304','F308','F309','F310','F3110','F3111','F3112','F3113','F312',
'F3130','F3131','F3132','F314','F315','F3160','F3161','F3162','F3163','F3164','F3170','F3171','F3172','F3173','F3174','F3175',
'F3176','F3177','F3178','F3181','F3189','F319','F320','F321','F322','F323','F324','F325','F328','F3289','F329','F32A','F330',
'F331','F332','F333','F3340','F3341','F3342','F338','F339','F340','F341','F3481','F3489','F349','F39','F4321','F4323')
	then dep_phy_clm=1;
/* Serious+ Persistent (Bipolar and Major) Depression ICD10 codes from Adepoju et al. JAMANO 202211 */
	if diag{i} in ('F3010','F3011','F3012','F3013','F302','F303','F304','F308','F309','F310','F3110','F3111','F3112','F3113','F312','F3130','F3131',
'F3132','F314','F315','F3160','F3161','F3162','F3163','F3164','F3170','F3171','F3172','F3173','F3174','F3175','F3176','F3177',
'F3178','F3181','F3189','F319','F320','F321','F322','F323','F324','F325','F3289','F329','F330','F331','F332','F333','F3340',
'F3341','F3342','F338','F339','F340','F341')
	then spdep_phy_clm=1;
/* Anxiety Disorders */
	if diag{i} in ('F064','F4000','F4001','F4002',
				   'F4010','F4011','F40210','F40218','F40220','F40228','F40230','F40231','F40232','F40233','F40240','F40241','F40242','F40243',
				   'F40248','F40290','F40291','F40298','F408','F409','F410','F411','F413','F418','F419','F42','F422','F423','F424','F428','F429',
				   'F430','F4310','F4311','F4312','F449','F458','F488','F489','F938','F99','R452','R455','R456','R457')
	then anx_phy_clm=1;
end;

run;

proc sort data = phy_&YEAR.;
by BASEID CLAIMID;
run;

proc summary data = phy_&YEAR.; /* taking care of the duplicate claims line items to make sure they don't count for >1 claim */
by BASEID CLAIMID;
var pvd_phy_clm alc_phy_clm drg_phy_clm id_phy_clm au_phy_clm cp_phy_clm ad_phy_clm dep_phy_clm spdep_phy_clm anx_phy_clm;
output out=phy_&YEAR. (keep=BASEID CLAIMID pvd_phy_clm alc_phy_clm drg_phy_clm id_phy_clm au_phy_clm cp_phy_clm ad_phy_clm dep_phy_clm spdep_phy_clm anx_phy_clm)sum=;
run;

data phy_&YEAR.;
set phy_&YEAR.;
if pvd_phy_clm>1 then pvd_phy_clm=1;
if alc_phy_clm>1 then alc_phy_clm=1;
if drg_phy_clm>1 then drg_phy_clm=1;
if id_phy_clm>1 then id_phy_clm=1;
if au_phy_clm>1 then au_phy_clm=1;
if cp_phy_clm>1 then cp_phy_clm=1;
if ad_phy_clm>1 then ad_phy_clm=1;
if dep_phy_clm>1 then dep_phy_clm=1; 
if spdep_phy_clm>1 then spdep_phy_clm=1;
if anx_phy_clm>1 then anx_phy_clm=1; 
run;

proc sort data = phy_&YEAR.;
by BASEID;
run;

proc summary data = phy_&YEAR.; /* now summing up the physician claims by beneficiary id after double counts have been removed */
by BASEID;
var pvd_phy_clm alc_phy_clm drg_phy_clm id_phy_clm au_phy_clm cp_phy_clm ad_phy_clm dep_phy_clm spdep_phy_clm anx_phy_clm;
output out=phy_&YEAR. (keep=BASEID pvd_phy_clm alc_phy_clm drg_phy_clm id_phy_clm au_phy_clm cp_phy_clm ad_phy_clm dep_phy_clm spdep_phy_clm anx_phy_clm)sum=;
run;

/* merging together the claims datasets with the diagnoses identifiers */
data diag_&YEAR. (keep = BASEID PVD_DX ALC_DX DRG_DX AD_DX AD_DX_CNT DP_DX SPDP_DX ANX_DX ID_DX ID_DX_CNT AU_DX AU_DX_CNT CP_DX CP_DX_CNT);
merge phy_&YEAR. otp_&YEAR. inp_&YEAR. snf_&YEAR. hha_&YEAR.;
by BASEID;
/* now apply CMS CCW condition algorithms to claims */
PVD_DX=0;
if (sum(pvd_phy_clm,pvd_otp_clm,pvd_snf_clm,pvd_hh_clm)) >= 2 or pvd_inp_clm >= 1 then PVD_DX = 1;  
ALC_DX = 0;
if (sum(alc_phy_clm,alc_otp_clm,alc_snf_clm,alc_hh_clm)) >= 2 or alc_inp_clm >= 1 then ALC_DX = 1; 
DRG_DX = 0;
if (sum(drg_phy_clm,drg_otp_clm,drg_snf_clm,drg_hh_clm)) >= 2 or drg_inp_clm >= 1 then DRG_DX = 1; 
AD_DX=0;
if (sum(ad_phy_clm,ad_otp_clm,ad_snf_clm,ad_hh_clm,ad_inp_clm))>=1 then AD_DX = 1;
AD_DX_CNT=0;
AD_DX_CNT=(sum(ad_phy_clm,ad_otp_clm,ad_snf_clm,ad_hh_clm,ad_inp_clm));
DP_DX=0;
if (sum(dep_phy_clm,dep_otp_clm)) >= 2 or (sum(dep_snf_clm,dep_hh_clm,dep_inp_clm)) >= 1 then DP_DX = 1;
SPDP_DX=0;
if spdep_phy_clm>=2 or spdep_otp_clm>=2 or spdep_snf_clm>=1 or spdep_hh_clm>=1 or spdep_inp_clm>=1 then SPDP_DX = 1;
ANX_DX = 0;
if (sum(anx_phy_clm,anx_otp_clm,anx_snf_clm,anx_hh_clm)) >= 2 or anx_inp_clm >= 1 then ANX_DX = 1; 
ID_DX=0;
if (sum(id_phy_clm,id_otp_clm,id_snf_clm,id_hh_clm)) >= 2 or id_inp_clm >= 1 then ID_DX = 1;
ID_DX_CNT=sum(id_phy_clm,id_otp_clm,id_snf_clm,id_hh_clm,id_inp_clm);
if ID_DX_CNT=. then ID_DX_CNT=0;
AU_DX=0;
if (sum(au_phy_clm,au_otp_clm,au_snf_clm,au_hh_clm)) >= 2 or au_inp_clm >= 1 then AU_DX = 1; 
AU_DX_CNT=sum(AU_phy_clm,AU_otp_clm,AU_snf_clm,AU_hh_clm,AU_inp_clm);
if AU_DX_CNT=. then AU_DX_CNT=0;
CP_DX=0;
if (sum(cp_phy_clm,cp_otp_clm,cp_snf_clm,cp_hh_clm)) >= 2 or cp_inp_clm >= 1 then CP_DX = 1; 
CP_DX_CNT=sum(CP_phy_clm,CP_otp_clm,CP_snf_clm,CP_hh_clm,CP_inp_clm);
if CP_DX_CNT=. then CP_DX_CNT=0;
run;

Data sample_&YEAR. (keep = BASEID CL_YR PTA_MM PTB_MM FFS_MM MA_MM MA_FLAG PRVSUP PRVSUP_MED PRVSUP_RX VA_FLAG H_MEDSTA NEW_BENE ESRD DISABLED OREC_AGE_ONLY MCAID_ANY MCAID_FULL MCAID_PART MC_ENT_ST MCR_ENT_YR MCR_ENT_MM SHSV_ACO1 NXGN_ACO CPCPL_PCMH PTD_MM 
						PD_FLAG PARTD_LIS HASPTD HASPTB HASHMO MA_INS_NM PTC_STR_RT MA_SNP MA_SNP_CD MA_SNP_DE MA_SNP_FIDE MA_SNP_IN H_DOB DOB AGE_0101 DIED DOD_YRMNDY AGE64 AGE74 AGE84 AGE85 USRD MBRZP MBRCTY MBRST SSA_SCTY RUCA1 ruca_metro1 ruca_micro1
						ruca_rural1 ADI_SCR NEWENGLAND MIDWEST WESTNORTHCENTRAL SOUTHATLANTIC EASTSCENTRAL WESTSCENTRAL MOUNTAIN PACIFIC MALE FEMALE WHITE BLACK HISPANIC ASIAN NTVAM HAWPAC MIXED OTHER 
						OTHR_LANG ENG_SPK_PRB EDUNKN EDNOHS EDHSGD EDSMCL EDCLGD INCOME FPL_LTE100 FPL_101_200 FPL_GT200 MARRIED WIDOW DIV_SEP NEV_MARRY VET_STS WORKING_SR MCBS_INST MDS3_INST NE MW SO WE HSPC 
						SNF_ST SNF_DY PTA_REIMB PTB_REIMB TOT_REIMB PTD_REIMB SHSV_ACO SV_VARSTRATUM SV_VARPSU SV_XSEE_WGT CS_VARSTRATUM CS_VARPSU CS_XSEE_WGT ALZDMT_SR DPRSSN_SR
						DM_RP DM_T1 DM_T2 DM_PRE HF_RP IH_RP ANGINA_RP CD_RP HYST_RP CLN_CNCR_RP IDD_RP PSYCH_RP ADL IADL COGIMP_SR DF_LIFT_RP DF_KNEL_RP DF_WALK_RP DF_DRES_RP DF_EAT_RP OVRWT OBESE HT_CM WT_KG BAD_SR_HLTH WRS_SR_HLTH 
						TBC_NOW PREV_SMKR ALC_PRB BLDCHL_RP BLDPRS_RP FLU_RP PNEUM_RP MAMM_RP PAPS_RP PSA_RP COL_FOBT_RP COL_FOBT_RP_LT5 COL_SCOP_RP COL_SCOP_RP_LT5 dm_ftexam dm_bldtst_self dm_slfmgt_ed dm_drvs dm_hba1c dm_cntrld PHQ8 PHQ8_NR PHQ2 PHQ2_NR GAD2 GAD2_NR PHQ_DP_DIF PRB_SLEEP MED_BILS_COLLECT HC_CST_DELAY 
						TRB_GT_CARE GPS_SCR NOMD_CST MD_CARE_HLTH MD_TRVL_EASE SPC_ACCS_SAT QUAL_SAT INFO_SAT AFRD_RPLC_FOOD AFRD_EAT_HLTH NOAFRD_EAT_LESS NOAFRD_HUNGRY OWN_HOME SECT_EGHT HOME_VALU 
						LIVE_ALON N_HHLD CS_USC_XSEE_WGT SV_USC_XSEE_WGT TUP_SCR USOC USOC_PCP USOC_SPC SPC_SR PCP_SPC_COORD USOC_VS TEST_SR TEST_FLWP_SR NEED_HOME_SR HELP_HOME_SR RX_SR INFO_RX_SR HEAR_PRB HEAR_AID CS_MED_XSEE_WGT SV_MED_XSEE_WGT SR_ED_VS_IND SR_OP_VS_IND SR_MD_VS_IND SR_MD_PCP_VS_IND SR_MD_SPC_VS_IND LTC_days LTC_SNF_days HH_VS CU_HH_CLM CU_IP_ST CU_IP_DY CU_SNF_ST CU_SNF_DY CU_ED_VS CU_CLN_VS MCAID_REIMB RX_ADRD_COV_DYS RX_ADRD_PDC 
						RX_ANDP_COV_DYS RX_ANDP_PDC AD_DX_CCW AD_DX_PRYR AMI_DX CD_DX AFB_DX BRC_DX CRC_DX ENC_DX LUC_DX PRC_DX HF_DX CKD_DX DP_DX_CCW DP_DX_CCW_DT SPDP_DX_CCW DM_DX DSL_DX HTN_DX IH_DX ART_DX SRK_DX ANX_DX_CCW ANX_DX_CCW_DT SP_DX_CCW HIV_DX ID_DX_CCW PVD_DX ALC_DX DRG_DX AD_DX AD_DX_CNT DP_DX SPDP_DX ANX_DX ID_DX ID_DX_CNT AU_DX AU_DX_CNT CP_DX CP_DX_CNT AD DM HF IH CD DP);
merge sample_&YEAR. diag_&YEAR.;
by baseid;
if CL_YR = &YEAR.;
if AD_DX_CCW=. then AD_DX_CCW=0;
if AD_DX_PRYR=. then AD_DX_PRYR=0;
if AD_DX=. then AD_DX=0;
if AD_DX_CNT=. then AD_DX_CNT=0;
if ALZDMT_SR = 1 or AD_DX_CCW = 1 or AD_DX_PRYR=1 or AD_DX = 1 then AD = 1; else AD = 0;
if DP_DX_CCW=. then DP_DX_CCW=0;
if SPDP_DX_CCW=. then SPDP_DX_CCW=0;
if DP_DX=. then DP_DX=0;
if SPDP_DX=. then SPDP_DX=0;
if ANX_DX_CCW=. then ANX_DX_CCW=0;
if ANX_DX=. then ANX_DX=0;

if DM_DX=. then DM_DX=0; if DM_RP = 1 or DM_DX = 1 then DM = 1; else DM = 0;
if HF_DX=. then HF_DX=0; if HF_RP = 1 or HF_DX = 1 then HF = 1; else HF = 0;
if IH_DX=. then IH_DX=0; if IH_RP = 1 or IH_DX = 1 then IH = 1; else IH = 0;
if CD_DX=. then CD_DX=0; if CD_RP = 1 or CD_DX = 1 then CD = 1; else CD = 0;
if DPRSSN_SR = 1 or DP_DX_CCW = 1 or DP_DX = 1 then DP = 1; else DP = 0;

if HTN_DX=. then HTN_DX=0;
if DSL_DX=. then DSL_DX=0;
if AMI_DX=. then AMI_DX=0;
if SRK_DX=. then SRK_DX=0;
if PVD_DX=. then PVD_DX=0;
if AFB_DX=. then AFB_DX=0;
if CKD_DX=. then CKD_DX=0;
if CRC_DX=. then CRC_DX=0;
if ENC_DX=. then ENC_DX=0;
if BRC_DX=. then BRC_DX=0;
if LUC_DX=. then LUC_DX=0;
if PRC_DX=. then PRC_DX=0;
if ART_DX=. then ART_DX=0;
if ALC_DX=. then ALC_DX=0;
if DRG_DX=. then DRG_DX=0;
if ID_DX=. then ID_DX=0;
if ID_DX_CNT=. then ID_DX_CNT=0;
if AU_DX=. then AU_DX=0;
if AU_DX_CNT=. then AU_DX_CNT=0;
if CP_DX=. then CP_DX=0;
if CP_DX_CNT=. then CP_DX_CNT=0;

run;

/* 17 - Gather claims data to run the HCC and Charlson */

/* getting diagnoses for inpatient acute care hospitalizations */
Data inp_&YEAR. (keep = BASEID CLM_FROM_DT dx01 dx02 dx03 dx04 dx05 dx06 dx07 dx08 dx09 dx10 dx11 dx12 dx13 dx14 dx15 dx16 dx17 dx18 dx19 dx20 dx21 dx22 dx23 dx24 dx25);																
set claims&YR..inp;	
	
dx01=ICD_DGNS_CD1; dx02=ICD_DGNS_CD2; dx03=ICD_DGNS_CD3; dx04=ICD_DGNS_CD4; dx05=ICD_DGNS_CD5; dx06=ICD_DGNS_CD6; dx07=ICD_DGNS_CD7; dx08=ICD_DGNS_CD8; dx09=ICD_DGNS_CD9; 
dx10=ICD_DGNS_CD10; dx11=ICD_DGNS_CD11; dx12=ICD_DGNS_CD12; dx13=ICD_DGNS_CD13; dx14=ICD_DGNS_CD14; dx15=ICD_DGNS_CD15; dx16=ICD_DGNS_CD16; dx17=ICD_DGNS_CD17; dx18=ICD_DGNS_CD18; 
dx19=ICD_DGNS_CD19; dx20=ICD_DGNS_CD20; dx21=ICD_DGNS_CD21; dx22=ICD_DGNS_CD22; dx23=ICD_DGNS_CD23; dx24=ICD_DGNS_CD24; dx25=ICD_DGNS_CD25; 	

c_dx01=put(dx01,$7.); drop dx01; rename c_dx01=dx01;
c_dx02=put(dx02,$7.); drop dx02; rename c_dx02=dx02;
c_dx03=put(dx03,$7.); drop dx03; rename c_dx03=dx03;
c_dx04=put(dx04,$7.); drop dx04; rename c_dx04=dx04;
c_dx05=put(dx05,$7.); drop dx05; rename c_dx05=dx05;
c_dx06=put(dx06,$7.); drop dx06; rename c_dx06=dx06;
c_dx07=put(dx07,$7.); drop dx07; rename c_dx07=dx07;
c_dx08=put(dx08,$7.); drop dx08; rename c_dx08=dx08;
c_dx09=put(dx09,$7.); drop dx09; rename c_dx09=dx09;	
c_dx10=put(dx10,$7.); drop dx10; rename c_dx10=dx10;	
c_dx11=put(dx11,$7.); drop dx11; rename c_dx11=dx11;
c_dx12=put(dx12,$7.); drop dx12; rename c_dx12=dx12;
c_dx13=put(dx13,$7.); drop dx13; rename c_dx13=dx13;
c_dx14=put(dx14,$7.); drop dx14; rename c_dx14=dx14;
c_dx15=put(dx15,$7.); drop dx15; rename c_dx15=dx15;
c_dx16=put(dx16,$7.); drop dx16; rename c_dx16=dx16;
c_dx17=put(dx17,$7.); drop dx17; rename c_dx17=dx17;
c_dx18=put(dx18,$7.); drop dx18; rename c_dx18=dx18;
c_dx19=put(dx19,$7.); drop dx19; rename c_dx19=dx19;
c_dx20=put(dx20,$7.); drop dx20; rename c_dx20=dx20;
c_dx21=put(dx21,$7.); drop dx21; rename c_dx21=dx21;
c_dx22=put(dx22,$7.); drop dx22; rename c_dx22=dx22;
c_dx23=put(dx23,$7.); drop dx23; rename c_dx23=dx23;
c_dx24=put(dx24,$7.); drop dx24; rename c_dx24=dx24;
c_dx25=put(dx25,$7.); drop dx25; rename c_dx25=dx25;
	
run;
   
/* getting diagnoses for outpatient (emergency department) services */																
Data otp_&YEAR. (keep = BASEID CLM_FROM_DT CLAIMID dx01 dx02 dx03 dx04 dx05 dx06 dx07 dx08 dx09 dx10 dx11 dx12 dx13 dx14 dx15 dx16 dx17 dx18 dx19 dx20 dx21 dx22 dx23 dx24 dx25);																
set claims&YR..otp;	
	
dx01=ICD_DGNS_CD1; dx02=ICD_DGNS_CD2; dx03=ICD_DGNS_CD3; dx04=ICD_DGNS_CD4; dx05=ICD_DGNS_CD5; dx06=ICD_DGNS_CD6; dx07=ICD_DGNS_CD7; dx08=ICD_DGNS_CD8; dx09=ICD_DGNS_CD9; 
dx10=ICD_DGNS_CD10; dx11=ICD_DGNS_CD11; dx12=ICD_DGNS_CD12; dx13=ICD_DGNS_CD13; dx14=ICD_DGNS_CD14; dx15=ICD_DGNS_CD15; dx16=ICD_DGNS_CD16; dx17=ICD_DGNS_CD17; dx18=ICD_DGNS_CD18; 
dx19=ICD_DGNS_CD19; dx20=ICD_DGNS_CD20; dx21=ICD_DGNS_CD21; dx22=ICD_DGNS_CD22; dx23=ICD_DGNS_CD23; dx24=ICD_DGNS_CD24; dx25=ICD_DGNS_CD25; 

c_dx01=put(dx01,$7.); drop dx01; rename c_dx01=dx01;
c_dx02=put(dx02,$7.); drop dx02; rename c_dx02=dx02;
c_dx03=put(dx03,$7.); drop dx03; rename c_dx03=dx03;
c_dx04=put(dx04,$7.); drop dx04; rename c_dx04=dx04;
c_dx05=put(dx05,$7.); drop dx05; rename c_dx05=dx05;
c_dx06=put(dx06,$7.); drop dx06; rename c_dx06=dx06;
c_dx07=put(dx07,$7.); drop dx07; rename c_dx07=dx07;
c_dx08=put(dx08,$7.); drop dx08; rename c_dx08=dx08;
c_dx09=put(dx09,$7.); drop dx09; rename c_dx09=dx09;	
c_dx10=put(dx10,$7.); drop dx10; rename c_dx10=dx10;	
c_dx11=put(dx11,$7.); drop dx11; rename c_dx11=dx11;
c_dx12=put(dx12,$7.); drop dx12; rename c_dx12=dx12;
c_dx13=put(dx13,$7.); drop dx13; rename c_dx13=dx13;
c_dx14=put(dx14,$7.); drop dx14; rename c_dx14=dx14;
c_dx15=put(dx15,$7.); drop dx15; rename c_dx15=dx15;
c_dx16=put(dx16,$7.); drop dx16; rename c_dx16=dx16;
c_dx17=put(dx17,$7.); drop dx17; rename c_dx17=dx17;
c_dx18=put(dx18,$7.); drop dx18; rename c_dx18=dx18;
c_dx19=put(dx19,$7.); drop dx19; rename c_dx19=dx19;
c_dx20=put(dx20,$7.); drop dx20; rename c_dx20=dx20;
c_dx21=put(dx21,$7.); drop dx21; rename c_dx21=dx21;
c_dx22=put(dx22,$7.); drop dx22; rename c_dx22=dx22;
c_dx23=put(dx23,$7.); drop dx23; rename c_dx23=dx23;
c_dx24=put(dx24,$7.); drop dx24; rename c_dx24=dx24;
c_dx25=put(dx25,$7.); drop dx25; rename c_dx25=dx25;
	
run;																
																
proc sort data = otp_&YEAR.; 																
by BASEID CLAIMID; 																
run;																
																
proc sort data = claims&YR..otprev; 																
by BASEID CLAIMID; 																
run;																
																
data otp_ed_&YEAR. (keep = BASEID CLM_FROM_DT dx01--dx25);																
merge otp_&YEAR. claims&YR..otprev; ;																
by BASEID CLAIMID;																
if dx01 ne ' '; /*member is in the otp file above */																
if REV_CNTR in ('0981','0450','0451','0452','0453','0454','0455','0456','0457','0458','0459'); /* ed visit identifier */ 																
run;				

/* getting diagnoses for ambulatory physician services - excluding radiology, pathology, and lab claims */																
Data phy_&YEAR. (keep = BASEID CLM_FROM_DT dx01 dx02 dx03 dx04 dx05 dx06 dx07 dx08 dx09 dx10 dx11 dx12);																
set claims&YR..phy;	

CLM_FROM_DT=CLM_THRU_DT;
	
dx01=ICD_DGNS_CD1; dx02=ICD_DGNS_CD2; dx03=ICD_DGNS_CD3; dx04=ICD_DGNS_CD4; dx05=ICD_DGNS_CD5; dx06=ICD_DGNS_CD6; dx07=ICD_DGNS_CD7; dx08=ICD_DGNS_CD8; dx09=ICD_DGNS_CD9; 
dx10=ICD_DGNS_CD10; dx11=ICD_DGNS_CD11; dx12=ICD_DGNS_CD12;

c_dx01=put(dx01,$7.); drop dx01; rename c_dx01=dx01;
c_dx02=put(dx02,$7.); drop dx02; rename c_dx02=dx02;
c_dx03=put(dx03,$7.); drop dx03; rename c_dx03=dx03;
c_dx04=put(dx04,$7.); drop dx04; rename c_dx04=dx04;
c_dx05=put(dx05,$7.); drop dx05; rename c_dx05=dx05;
c_dx06=put(dx06,$7.); drop dx06; rename c_dx06=dx06;
c_dx07=put(dx07,$7.); drop dx07; rename c_dx07=dx07;
c_dx08=put(dx08,$7.); drop dx08; rename c_dx08=dx08;
c_dx09=put(dx09,$7.); drop dx09; rename c_dx09=dx09;	
c_dx10=put(dx10,$7.); drop dx10; rename c_dx10=dx10;	
c_dx11=put(dx11,$7.); drop dx11; rename c_dx11=dx11;
c_dx12=put(dx12,$7.); drop dx12; rename c_dx12=dx12;
	
if BETOS_CD in ('M1A','M1B','M4A','M4B','M5C','M5D','M6');    /* limiting to ambulatory E&M visits */																
if PRVDR_SPCLTY not in ('36','22','30','31','94','69','95','47','63','45');  /*removing pathology, laboratory, and radiology */																
run;			


/* setting together the claims datasets with the diagnoses identifiers */																
data diag_clms_&YEAR.;																
set phy_&YEAR. otp_ed_&YEAR. inp_&YEAR.;																																
run;	

/* 15a - Running the Charlson Comorbidity Index for ICD10 */

%macro _CharlsonICD10 (DATA    =,     /* input data set */
                       OUT     =,     /* output data set */
                       dx      = , /* range of diagnosis variables (dx01-dx25) */
                       dxtype  =,/* range of diagnosis type variables */										
                                                  /* (dxtype01-dxtype25) */	
                       type    =, /** on/off turn on or off use of dxtype ***/	
                       debug   = ) ;
	%put Charlson Comorbidity Index Macro - ICD10 Codes ;
	%put Manitoba Centre for Health Policy, Based on Code from Hude Quan University of Calgary ;
	%put Quan et al., Coding Algorithms for Defining Comorbidities ;
	%put     in ICD-9-CM and ICD-10 Administrative Data, Medical Care:43(11), Nov. 2005 p1130-1139 ;
	%put Version 1.0e February 23, 2007 ;


	%let debug = %lowcase(&debug) ;
	%let type = %lowcase(&type) ;

	%* put default options into &opts variable ;
  %let opts=%sysfunc(getoption(mprint,keyword))
	  %sysfunc(getoption(notes,keyword)) ;

  %if &debug=1 | &debug=debug %then %do ;
		options mprint notes ;
		%end ;
	%else %do ;
		options nomprint nonotes ;
		%end ;
	
	%* Check if previous data step, or procdure had an error and
	 stop running the rates macro
	This assumes that the previous step is used in the macro.;
 %if %eval(&SYSERR>0) %then %goto out1 ; 
  
  %* Check if input data exists ;
  %if &data= %str() %then %goto out2 ;
  
  %* if the output data set is not defined then define it as the input ;
  %if &out=  %then %let out=&data ;
  
  %if %index(&data,.) %then %do;
     %let libname=%scan(&data,1);
	 %let data=%scan(&data,2);
	 %end;
 %else %do ;
	 %let libname=work ;
	 %let data=&data ;
	 %end ;
 
 %if %sysfunc(exist(&libname..&data)) ^= 1 %then %goto out3 ;	

      data &OUT;
      set &DATA ;

      /*  set up array for individual CCI group counters */
      array CC_GRP (17) CC_GRP_1 - CC_GRP_17;

      /*  set up array for 25 diagnosis codes within a record  */
      array DX (*) &dx;

      /*  set up array for 25 diagnosis type codes within a record */
	  %if &type=on %then array DXTYPE (*) &dxtype; ;
	  

      /*  initialize all CCI group counters to zero */
      do i = 1 to 17;
         CC_GRP(i) = 0;
      end;

      /*  check each patient record for the diagnosis codes in each CCI group */
      do i = 1 to dim(dx) UNTIL (DX(i)=' ');     /* for each set of diagnoses codes */

        /*  skip diagnosis if diagnosis type = "2" */
		  %if &type=on %then if DXTYPE(i) ^="2" then DO; ;
		  

           /* Myocardial Infarction */
           if DX(i) IN: ('I21', 'I22','I252') then CC_GRP_1 = 1;
           LABEL CC_GRP_1 = 'Myocardial Infarction';

           /* Congestive Heart Failure */
           if DX(i) IN: ('I43','I50','I099','I110','I130','I132','I255','I420','I425','I426',
                         'I427','I428','I429','P290') then CC_GRP_2 = 1;
           LABEL CC_GRP_2 = 'Congestive Heart Failure';

           /* Periphral Vascular Disease */
           if DX(i) IN: ('I70','I71','I731','I738','I739','I771','I790','I792','K551','K558',
                         'K559','Z958','Z959') then CC_GRP_3 = 1;
           LABEL CC_GRP_3 = 'Periphral Vascular Disease';

           /* Cerebrovascular Disease */
           if DX(i) IN: ('G45','G46','I60','I61','I62','I63','I64','I65','I66','I67','I68',
                         'I69','H340') then CC_GRP_4 = 1;
           LABEL CC_GRP_4 = 'Cerebrovascular Disease';

           /* Dementia */
           if DX(i) IN: ('F00','F01','F02','F03','G30','F051','G311')
                         then CC_GRP_5 = 1;
           LABEL CC_GRP_5 = 'Dementia';

           /* Chronic Pulmonary Disease */
           if DX(i) IN: ('J40','J41','J42','J43','J44','J45','J46','J47','J60','J61','J62','J63',
                         'J64','J65','J66','J67''I278','I279','J684','J701','J703')
                         then CC_GRP_6 = 1;
           LABEL CC_GRP_6 = 'Chronic Pulmonary Disease';

           /* Connective Tissue Disease-Rheumatic Disease */
           if DX(i) IN: ('M05','M32','M33','M34','M06','M315','M351','M353','M360')
                         then CC_GRP_7 = 1;
           LABEL CC_GRP_7 = 'Connective Tissue Disease-Rheumatic Disease';

           /* Peptic Ulcer Disease */
           if DX(i) IN: ('K25','K26','K27','K28') then CC_GRP_8 = 1;
           LABEL CC_GRP_8 = 'Peptic Ulcer Disease';

           /* Mild Liver Disease */
           if DX(i) IN: ('B18','K73','K74','K700','K701','K702','K703','K709','K717','K713',
                         'K714','K715','K760','K762','K763','K764','K768','K769','Z944')
                         then CC_GRP_9 = 1;
           LABEL CC_GRP_9 = 'Mild Liver Disease';

           /* Diabetes without complications */
           if DX(i) IN: ('E100','E101','E106','E108','E109','E110','E111','E116','E118','E119',
                         'E120','E121','E126','E128','E129','E130','E131','E136','E138','E139',
                         'E140','E141','E146','E148','E149') then CC_GRP_10 = 1;
           LABEL CC_GRP_10 = 'Diabetes without complications';

           /* Diabetes with complications */
           if DX(i) IN: ('E102','E103','E104','E105','E107','E112','E113','E114','E115','E117',
                         'E122','E123','E124','E125','E127','E132','E133','E134','E135','E137',
                         'E142','E143','E144','E145','E147') then CC_GRP_11 = 1;
           LABEL CC_GRP_11 = 'Diabetes with complications';

           /* Paraplegia and Hemiplegia */
           if DX(i) IN: ('G81','G82','G041','G114','G801','G802','G830','G831','G832','G833',
                         'G834','G839') then CC_GRP_12 = 1;
           LABEL CC_GRP_12 = 'Paraplegia and Hemiplegia';

           /* Renal Disease */
           if DX(i) IN: ('N18','N19','N052','N053','N054','N055','N056','N057','N250','I120',
                         'I131','N032','N033','N034','N035','N036','N037','Z490','Z491','Z492',
                         'Z940','Z992') then CC_GRP_13 = 1;
           LABEL CC_GRP_13 = 'Renal Disease';

           /* Cancer */
           if DX(i) IN: ('C00','C01','C02','C03','C04','C05','C06','C07','C08','C09','C10','C11',
                         'C12','C13','C14','C15','C16','C17','C18','C19','C20','C21','C22','C23',
                         'C24','C25','C26','C30','C31','C32','C33','C34','C37','C38','C39','C40',
                         'C41','C43','C45','C46','C47','C48','C49','C50','C51','C52','C53','C54',
                         'C55','C56','C57','C58','C60','C61','C62','C63','C64','C65','C66','C67',
                         'C68','C69','C70','C71','C72','C73','C74','C75','C76','C81','C82','C83',
                         'C84','C85','C88','C90','C91','C92','C93','C94','C95','C96','C97')
                         then CC_GRP_14 = 1;
           LABEL CC_GRP_14 = 'Cancer';

           /* Moderate or Severe Liver Disease */
           if DX(i) IN: ('K704','K711','K721','K729','K765','K766','K767','I850','I859','I864','I982')
                         then CC_GRP_15 = 1;
           LABEL CC_GRP_15 = 'Moderate or Severe Liver Disease';

           /* Metastatic Carcinoma */
           if DX(i) IN: ('C77','C78','C79','C80') then CC_GRP_16 = 1;
           LABEL CC_GRP_16 = 'Metastatic Carcinoma';

           /* AIDS/HIV */
           if DX(i) IN: ('B20','B21','B22','B24') then CC_GRP_17 = 1;
           LABEL CC_GRP_17 = 'AIDS/HIV';

        %if &type=on %then end; ;                 /* end if DXTYPE(i) ^= '2' */

      end;                   /* end do i = 1 to 25 */

      /* Count total number of groups for each record */

     TOT_GRP = CC_GRP_1  + CC_GRP_2  + CC_GRP_3  + CC_GRP_4  + CC_GRP_5  + CC_GRP_6  + CC_GRP_7  + CC_GRP_8  +
               CC_GRP_9  + CC_GRP_10 + CC_GRP_11 + CC_GRP_12 + CC_GRP_13 + CC_GRP_14 + CC_GRP_15 + CC_GRP_16 +
               CC_GRP_17;
     LABEL TOT_GRP = 'Total CCI Groups per record';

	run;
    
	options notes ;
	 %put ;
	 %put NOTE: _Charlson Finished &out created ;
     %put ;	

	%goto exit ;
	
    %out1:
		%put ERROR: Prior Step failed with an Error submit a null data step to correct ;
	%goto exit ;

	%out2:
		%put ERROR: Input Data Was Not Defined;
	%goto exit ;

    %out3:
        %put ERROR: Input Data &libname..&data does not exist ;
        %goto exit ;	

    %exit:
	
     %**** Reset the SAS options ;
     options &opts ; 

%mend _CharlsonICD10;

/* Call Statement - run macro above first before doing the call */	

  %_CharlsonICD10 (DATA      =diag_clms_&YEAR., /* input data set */	
                   OUT       =diag_clms_&YEAR.b, /* output data set */
                   dx        =dx01-dx25, /* range of diagnosis variables (dx01) */
                   dxtype    =dxtype01-dxtype25,/* range of diagnosis type  variables */								
                                                  /* (dxtype01-dxtype25) */	
                   type      =off, /** on/off turn on or off use of dxtype								
 ***/				
                   debug     =off) ;	

/* Setting up the CCI Dataset */							
Data diag_clms_&YEAR.b (keep = BASEID CC_GRP_1--TOT_GRP);								
set diag_clms_&YEAR.b;								
run;								
								
proc sort data = diag_clms_&YEAR.b;								
by BASEID;								
run;								
								
proc summary data = diag_clms_&YEAR.b;								
by BASEID;								
var CC_GRP_1--TOT_GRP;								
output out = cci_diag_clms_&YEAR. (keep = BASEID CC_GRP_1--TOT_GRP)sum=;								
run;								
								
data cci_diag_clms_&YEAR. (keep = BASEID LIV_DX CAN_DX CCI_Score);								
set cci_diag_clms_&YEAR.;	

if CC_GRP_9 >0 or CC_GRP_15 >0 then LIV_DX=1; else LIV_DX=0;
if CC_GRP_14 >0 or CC_GRP_16 >0 then CAN_DX=1; else CAN_DX=0;
	
if CC_GRP_1 >0 then CC_GRP_1_sc = 1; else CC_GRP_1_sc = 0;								
if CC_GRP_2 >0 then CC_GRP_2_sc = 2; else CC_GRP_2_sc = 0;								
if CC_GRP_3 >0 then CC_GRP_3_sc = 1; else CC_GRP_3_sc = 0;								
if CC_GRP_4 >0 then CC_GRP_4_sc = 1; else CC_GRP_4_sc = 0;								
if CC_GRP_5 >0 then CC_GRP_5_sc = 3; else CC_GRP_5_sc = 0;								
if CC_GRP_6 >0 then CC_GRP_6_sc = 2; else CC_GRP_6_sc = 0;								
if CC_GRP_7 >0 then CC_GRP_7_sc = 0; else CC_GRP_7_sc = 0;								
if CC_GRP_8 >0 then CC_GRP_8_sc = 0; else CC_GRP_8_sc = 0;								
if CC_GRP_9 >0 then CC_GRP_9_sc = 2; else CC_GRP_9_sc = 0;								
if CC_GRP_10 >0 then CC_GRP_10_sc = 1; else CC_GRP_10_sc = 0;								
if CC_GRP_11 >0 then CC_GRP_11_sc = 2; else CC_GRP_11_sc = 0;								
if CC_GRP_12 >0 then CC_GRP_12_sc = 1; else CC_GRP_12_sc = 0;								
if CC_GRP_13 >0 then CC_GRP_13_sc = 3; else CC_GRP_13_sc = 0;								
if CC_GRP_14 >0 then CC_GRP_14_sc = 2; else CC_GRP_14_sc = 0;								
if CC_GRP_15 >0 then CC_GRP_15_sc = 4; else CC_GRP_15_sc = 0;								
if CC_GRP_16 >0 then CC_GRP_16_sc = 6; else CC_GRP_16_sc = 0;								
if CC_GRP_17 >0 then CC_GRP_17_sc = 4; else CC_GRP_17_sc = 0;													
CCI_Score = CC_GRP_1_sc + CC_GRP_2_sc + CC_GRP_3_sc + CC_GRP_4_sc +	CC_GRP_5_sc + CC_GRP_6_sc								
+ CC_GRP_7_sc + CC_GRP_8_sc + CC_GRP_9_sc + CC_GRP_10_sc + CC_GRP_11_sc + CC_GRP_12_sc + CC_GRP_13_sc 								
+ CC_GRP_14_sc + CC_GRP_15_sc + CC_GRP_16_sc + CC_GRP_17_sc;

Run;	

Data sample_&YEAR. (keep = BASEID CL_YR PTA_MM PTB_MM FFS_MM MA_MM MA_FLAG PRVSUP PRVSUP_MED PRVSUP_RX VA_FLAG H_MEDSTA NEW_BENE ESRD DISABLED OREC_AGE_ONLY MCAID_ANY MCAID_FULL MCAID_PART MC_ENT_ST MCR_ENT_YR MCR_ENT_MM SHSV_ACO1 NXGN_ACO CPCPL_PCMH PTD_MM 
						PD_FLAG PARTD_LIS HASPTD HASPTB HASHMO MA_INS_NM PTC_STR_RT MA_SNP MA_SNP_CD MA_SNP_DE MA_SNP_FIDE MA_SNP_IN H_DOB DOB AGE_0101 DIED DOD_YRMNDY AGE64 AGE74 AGE84 AGE85 USRD MBRZP MBRCTY MBRST SSA_SCTY RUCA1 ruca_metro1 ruca_micro1
						ruca_rural1 ADI_SCR NEWENGLAND MIDWEST WESTNORTHCENTRAL SOUTHATLANTIC EASTSCENTRAL WESTSCENTRAL MOUNTAIN PACIFIC MALE FEMALE WHITE BLACK HISPANIC ASIAN NTVAM HAWPAC MIXED OTHER 
						OTHR_LANG ENG_SPK_PRB EDUNKN EDNOHS EDHSGD EDSMCL EDCLGD INCOME FPL_LTE100 FPL_101_200 FPL_GT200 MARRIED WIDOW DIV_SEP NEV_MARRY VET_STS WORKING_SR MCBS_INST MDS3_INST NE MW SO WE HSPC 
						SNF_ST SNF_DY PTA_REIMB PTB_REIMB TOT_REIMB PTD_REIMB SHSV_ACO SV_VARSTRATUM SV_VARPSU SV_XSEE_WGT CS_VARSTRATUM CS_VARPSU CS_XSEE_WGT ALZDMT_SR DPRSSN_SR
						DM_RP DM_T1 DM_T2 DM_PRE HF_RP IH_RP ANGINA_RP CD_RP HYST_RP CLN_CNCR_RP IDD_RP PSYCH_RP ADL IADL COGIMP_SR DF_LIFT_RP DF_KNEL_RP DF_WALK_RP DF_DRES_RP DF_EAT_RP OVRWT OBESE HT_CM WT_KG BAD_SR_HLTH WRS_SR_HLTH 
						TBC_NOW PREV_SMKR ALC_PRB BLDCHL_RP BLDPRS_RP FLU_RP PNEUM_RP MAMM_RP PAPS_RP PSA_RP COL_FOBT_RP COL_FOBT_RP_LT5 COL_SCOP_RP COL_SCOP_RP_LT5 dm_ftexam dm_bldtst_self dm_slfmgt_ed dm_drvs dm_hba1c dm_cntrld PHQ8 PHQ8_NR PHQ2 PHQ2_NR GAD2 GAD2_NR PHQ_DP_DIF PRB_SLEEP MED_BILS_COLLECT HC_CST_DELAY 
						TRB_GT_CARE GPS_SCR NOMD_CST MD_CARE_HLTH MD_TRVL_EASE SPC_ACCS_SAT QUAL_SAT INFO_SAT AFRD_RPLC_FOOD AFRD_EAT_HLTH NOAFRD_EAT_LESS NOAFRD_HUNGRY OWN_HOME SECT_EGHT HOME_VALU 
						LIVE_ALON N_HHLD CS_USC_XSEE_WGT SV_USC_XSEE_WGT TUP_SCR USOC USOC_PCP USOC_SPC SPC_SR PCP_SPC_COORD USOC_VS TEST_SR TEST_FLWP_SR NEED_HOME_SR HELP_HOME_SR RX_SR INFO_RX_SR HEAR_PRB HEAR_AID CS_MED_XSEE_WGT SV_MED_XSEE_WGT SR_ED_VS_IND SR_OP_VS_IND SR_MD_VS_IND SR_MD_PCP_VS_IND SR_MD_SPC_VS_IND LTC_days LTC_SNF_days HH_VS CU_HH_CLM CU_IP_ST CU_IP_DY CU_SNF_ST CU_SNF_DY CU_ED_VS CU_CLN_VS MCAID_REIMB RX_ADRD_COV_DYS RX_ADRD_PDC 
						RX_ANDP_COV_DYS RX_ANDP_PDC AD_DX_CCW AD_DX_PRYR AMI_DX CD_DX AFB_DX BRC_DX CRC_DX ENC_DX LUC_DX PRC_DX HF_DX CKD_DX DP_DX_CCW DP_DX_CCW_DT SPDP_DX_CCW DM_DX DSL_DX HTN_DX IH_DX ART_DX SRK_DX ANX_DX_CCW ANX_DX_CCW_DT SP_DX_CCW HIV_DX ID_DX_CCW PVD_DX ALC_DX DRG_DX AD_DX AD_DX_CNT DP_DX SPDP_DX ANX_DX ID_DX ID_DX_CNT AU_DX AU_DX_CNT CP_DX CP_DX_CNT AD DM HF IH CD DP	LIV_DX CAN_DX CCI_Score);
merge sample_&YEAR. cci_diag_clms_&YEAR.;
by baseid;
if CL_YR = &YEAR.;
if CCI_Score =. then CCI_Score =0;
if HIV_DX =. then HIV_DX =0;
if LIV_DX =. then LIV_DX =0;
if CAN_DX =. then CAN_DX =0;
run;


/* 17b - Now Running the HCC Score */

/* CMS HCC Risk Score for 64-bit SAS */

/*you must specify the location of the files in a libname/filename statement */

  LIBNAME LIBRARY "R:\Active\mcbs\SAS\hcc\hcc_v22_2017";
  FILENAME IN0 "R:\Active\mcbs\SAS\hcc\hcc_v22_2017";
  LIBNAME  IN1 "R:\Active\mcbs\SAS\data\temp";
  LIBNAME  IN2 "R:\Active\mcbs\SAS\data\temp";
  LIBNAME  INCOEF "R:\Active\mcbs\SAS\data\temp";
  LIBNAME  OUT "R:\Active\mcbs\SAS\data\temp";

filename inc "R:\Active\mcbs\SAS\hcc\hcc_v22_2017\C2214O5P";
libname incoef "R:\Active\mcbs\SAS\data\temp";
proc cimport data=incoef.hcccoefn infile=inc;
run;

filename inf "R:\Active\mcbs\SAS\hcc\hcc_v22_2017\F2217O1P";
libname library "R:\Active\mcbs\SAS\data\temp";
proc cimport library=library infile=inf;
run;

data IN1.PERSON (keep = BASEID SEX DOB LTIMCAID NEMCAID OREC);
set sample_&YEAR.;
if MALE = 1 then SEX = '1'; else SEX = '2';
/*DOB2 = H_DOB; DOB = input(DOB2, yymmdd8.); format DOB mmddyy10.; */
if MCD_IND=1 then LTIMCAID=1; else LTIMCAID=0;
if MCD_IND=1 and  NEW_BENE=1 then NEMCAID=1; else NEMCAID=0;
if H_MEDSTA = '10' then OREC = '0'; 
else if H_MEDSTA = '20' then OREC = '1';
else if H_MEDSTA in ('11','31') then OREC = '2';
else if H_MEDSTA = '21' then OREC = '3';
run;

proc sort data = IN1.PERSON; by BASEID; run;

data diag1_clms_&YEAR. (keep = BASEID DIAG); set diag_clms_&YEAR.; DIAG=dx01; run;
data diag2_clms_&YEAR. (keep = BASEID DIAG); set diag_clms_&YEAR.; DIAG=dx02; run;
data diag3_clms_&YEAR. (keep = BASEID DIAG); set diag_clms_&YEAR.; DIAG=dx03; run;
data diag4_clms_&YEAR. (keep = BASEID DIAG); set diag_clms_&YEAR.; DIAG=dx04; run;
data diag5_clms_&YEAR. (keep = BASEID DIAG); set diag_clms_&YEAR.; DIAG=dx05; run;
data diag6_clms_&YEAR. (keep = BASEID DIAG); set diag_clms_&YEAR.; DIAG=dx06; run;
data diag7_clms_&YEAR. (keep = BASEID DIAG); set diag_clms_&YEAR.; DIAG=dx07; run;
data diag8_clms_&YEAR. (keep = BASEID DIAG); set diag_clms_&YEAR.; DIAG=dx08; run;
data diag9_clms_&YEAR. (keep = BASEID DIAG); set diag_clms_&YEAR.; DIAG=dx09; run;
data diag10_clms_&YEAR. (keep = BASEID DIAG); set diag_clms_&YEAR.; DIAG=dx10; run;
data diag11_clms_&YEAR. (keep = BASEID DIAG); set diag_clms_&YEAR.; DIAG=dx11; run;
data diag12_clms_&YEAR. (keep = BASEID DIAG); set diag_clms_&YEAR.; DIAG=dx12; run;
data diag13_clms_&YEAR. (keep = BASEID DIAG); set diag_clms_&YEAR.; DIAG=dx13; run;
data diag14_clms_&YEAR. (keep = BASEID DIAG); set diag_clms_&YEAR.; DIAG=dx14; run;
data diag15_clms_&YEAR. (keep = BASEID DIAG); set diag_clms_&YEAR.; DIAG=dx15; run;
data diag16_clms_&YEAR. (keep = BASEID DIAG); set diag_clms_&YEAR.; DIAG=dx16; run;
data diag17_clms_&YEAR. (keep = BASEID DIAG); set diag_clms_&YEAR.; DIAG=dx17; run;
data diag18_clms_&YEAR. (keep = BASEID DIAG); set diag_clms_&YEAR.; DIAG=dx18; run;
data diag19_clms_&YEAR. (keep = BASEID DIAG); set diag_clms_&YEAR.; DIAG=dx19; run;
data diag20_clms_&YEAR. (keep = BASEID DIAG); set diag_clms_&YEAR.; DIAG=dx20; run;
data diag21_clms_&YEAR. (keep = BASEID DIAG); set diag_clms_&YEAR.; DIAG=dx21; run;
data diag22_clms_&YEAR. (keep = BASEID DIAG); set diag_clms_&YEAR.; DIAG=dx22; run;
data diag23_clms_&YEAR. (keep = BASEID DIAG); set diag_clms_&YEAR.; DIAG=dx23; run;
data diag24_clms_&YEAR. (keep = BASEID DIAG); set diag_clms_&YEAR.; DIAG=dx24; run;
data diag25_clms_&YEAR. (keep = BASEID DIAG); set diag_clms_&YEAR.; DIAG=dx25; run;

data IN2.DIAG (keep = BASEID DIAG);
set diag1_clms_&YEAR. diag2_clms_&YEAR. diag3_clms_&YEAR. diag4_clms_&YEAR. diag5_clms_&YEAR. diag6_clms_&YEAR. diag7_clms_&YEAR. diag8_clms_&YEAR. diag9_clms_&YEAR. 
diag10_clms_&YEAR. diag11_clms_&YEAR. diag12_clms_&YEAR. diag13_clms_&YEAR. diag14_clms_&YEAR. diag15_clms_&YEAR. diag16_clms_&YEAR. diag17_clms_&YEAR. diag18_clms_&YEAR.
diag19_clms_&YEAR. diag20_clms_&YEAR. diag21_clms_&YEAR. diag22_clms_&YEAR. diag23_clms_&YEAR. diag24_clms_&YEAR. diag25_clms_&YEAR.;
if DIAG ne '';
run;

proc sort data = IN2.DIAG; by BASEID; run;

/* USER CUSTOMIZATION:
 *
 * A user must supply 2 files with the variables described above and
 * set the following parameters:
 *      INP      - SAS input person dataset
 *      IND      - SAS input diagnosis dataset
 *      OUTDATA  - SAS output dataset
 *      IDVAR    - name of person id variable (HICNO for medicare data)
 *      KEEPVAR  - variables to keep in the output dataset
 *      SEDITS   - a switch that controls whether to perform MCE edits
 *                 on ICD10: 1-YES, 0-NO
 *      DATE_ASOF- reference date to calculate age. Set to February 1  
 *                 of the payment year for consistency with CMS 
 ***********************************************************************; */

%LET INPUTVARS=%STR(SEX DOB LTIMCAID NEMCAID OREC);

 %*demographic variables;
 %LET DEMVARS  =%STR(AGEF ORIGDS DISABL
                     F0_34  F35_44 F45_54 F55_59 F60_64 F65_69
                     F70_74 F75_79 F80_84 F85_89 F90_94 F95_GT
                     M0_34  M35_44 M45_54 M55_59 M60_64 M65_69
                     M70_74 M75_79 M80_84 M85_89 M90_94 M95_GT
                     NEF0_34  NEF35_44 NEF45_54 NEF55_59 NEF60_64
                     NEF65    NEF66    NEF67    NEF68    NEF69
                     NEF70_74 NEF75_79 NEF80_84 NEF85_89 NEF90_94
                     NEF95_GT
                     NEM0_34  NEM35_44 NEM45_54 NEM55_59 NEM60_64
                     NEM65    NEM66    NEM67    NEM68    NEM69
                     NEM70_74 NEM75_79 NEM80_84 NEM85_89 NEM90_94
                     NEM95_GT);

 %*list of HCCs included in models;
 %LET HCCV22_list79 = %STR(      
      HCC1    HCC2    HCC6    HCC8    HCC9    HCC10   HCC11   HCC12 
      HCC17   HCC18   HCC19   HCC21   HCC22   HCC23   HCC27   HCC28
      HCC29   HCC33   HCC34   HCC35   HCC39   HCC40   HCC46   HCC47 
      HCC48                   HCC54   HCC55   HCC57   HCC58   HCC70 
      HCC71   HCC72   HCC73   HCC74   HCC75   HCC76   HCC77   HCC78 
      HCC79   HCC80   HCC82   HCC83   HCC84   HCC85   HCC86   HCC87 
      HCC88   HCC96   HCC99   HCC100  HCC103  HCC104  HCC106  HCC107
      HCC108  HCC110  HCC111  HCC112  HCC114  HCC115  HCC122  HCC124
      HCC134  HCC135  HCC136  HCC137  
      HCC157  HCC158                  HCC161  HCC162  HCC166  HCC167
      HCC169  HCC170  HCC173  HCC176  HCC186  HCC188  HCC189  
      );

 %*list of CCs that correspond to model HCCs;
 %LET CCV22_list79 = %STR(      
      CC1     CC2     CC6     CC8     CC9     CC10    CC11    CC12 
      CC17    CC18    CC19    CC21    CC22    CC23    CC27    CC28
      CC29    CC33    CC34    CC35    CC39    CC40    CC46    CC47 
      CC48                    CC54    CC55    CC57    CC58    CC70 
      CC71    CC72    CC73    CC74    CC75    CC76    CC77    CC78 
      CC79    CC80    CC82    CC83    CC84    CC85    CC86    CC87 
      CC88    CC96    CC99    CC100   CC103   CC104   CC106   CC107
      CC108   CC110   CC111   CC112   CC114   CC115   CC122   CC124
      CC134   CC135   CC136   CC137   
      CC157   CC158                   CC161   CC162   CC166   CC167
      CC169   CC170   CC173   CC176   CC186   CC188   CC189   
      );

 %LET SCOREVARS=%STR(SCORE_COMMUNITY_NA
                     SCORE_COMMUNITY_ND
                     SCORE_COMMUNITY_FBA
                     SCORE_COMMUNITY_FBD
                     SCORE_COMMUNITY_PBA
                     SCORE_COMMUNITY_PBD 
                     SCORE_INSTITUTIONAL
                     SCORE_NEW_ENROLLEE
                     SCORE_SNP_NEW_ENROLLEE);

 %* include main macro;
 %INCLUDE IN0(V2217O1M)/SOURCE2;

 %V2217O1M(INP      =IN1.PERSON,
           IND      =IN2.DIAG,
           OUTDATA  =OUT.PERSON_&YEAR.,
           IDVAR    =BASEID,
           KEEPVAR  =BASEID &INPUTVARS &SCOREVARS &DEMVARS 
                     &HCCV22_list79 &CCV22_list79, 
           SEDITS   =1,
           DATE_ASOF="1FEB2017"D);
proc means data = OUT.PERSON_&YEAR. n sum mean; var SCORE_COMMUNITY_NA; run;
Data HCC_&YEAR. (keep = BASEID SCORE_COMMUNITY_NA SCORE_COMMUNITY_ND SCORE_COMMUNITY_FBA SCORE_COMMUNITY_FBD SCORE_INSTITUTIONAL SCORE_NEW_ENROLLEE HCC_subst HCC_psych);
set OUT.PERSON_&YEAR.;

if HCC54=1 or HCC55=1 then HCC_subst=1; else HCC_subst=0;
if HCC57=1 or HCC58=1 then HCC_psych=1; else HCC_psych=0;
run;

proc sort data = HCC_&YEAR. nodupkey; by BASEID; run; /* should not be any dupes */

Data sample_&YEAR. (keep = BASEID CL_YR PTA_MM PTB_MM FFS_MM MA_MM MA_FLAG PRVSUP PRVSUP_MED PRVSUP_RX VA_FLAG H_MEDSTA NEW_BENE ESRD DISABLED OREC_AGE_ONLY MCAID_ANY MCAID_FULL MCAID_PART MC_ENT_ST MCR_ENT_YR MCR_ENT_MM SHSV_ACO1 NXGN_ACO CPCPL_PCMH PTD_MM 
						PD_FLAG PARTD_LIS HASPTD HASPTB HASHMO MA_INS_NM PTC_STR_RT MA_SNP MA_SNP_CD MA_SNP_DE MA_SNP_FIDE MA_SNP_IN H_DOB DOB AGE_0101 DIED DOD_YRMNDY AGE64 AGE74 AGE84 AGE85 USRD MBRZP MBRCTY MBRST SSA_SCTY RUCA1 ruca_metro1 ruca_micro1
						ruca_rural1 ADI_SCR NEWENGLAND MIDWEST WESTNORTHCENTRAL SOUTHATLANTIC EASTSCENTRAL WESTSCENTRAL MOUNTAIN PACIFIC MALE FEMALE WHITE BLACK HISPANIC ASIAN NTVAM HAWPAC MIXED OTHER 
						OTHR_LANG ENG_SPK_PRB EDUNKN EDNOHS EDHSGD EDSMCL EDCLGD INCOME FPL_LTE100 FPL_101_200 FPL_GT200 MARRIED WIDOW DIV_SEP NEV_MARRY VET_STS WORKING_SR MCBS_INST MDS3_INST NE MW SO WE HSPC 
						SNF_ST SNF_DY PTA_REIMB PTB_REIMB TOT_REIMB PTD_REIMB SHSV_ACO SV_VARSTRATUM SV_VARPSU SV_XSEE_WGT CS_VARSTRATUM CS_VARPSU CS_XSEE_WGT ALZDMT_SR DPRSSN_SR
						DM_RP DM_T1 DM_T2 DM_PRE HF_RP IH_RP ANGINA_RP CD_RP HYST_RP CLN_CNCR_RP IDD_RP PSYCH_RP ADL IADL COGIMP_SR DF_LIFT_RP DF_KNEL_RP DF_WALK_RP DF_DRES_RP DF_EAT_RP OVRWT OBESE HT_CM WT_KG BAD_SR_HLTH WRS_SR_HLTH 
						TBC_NOW PREV_SMKR ALC_PRB BLDCHL_RP BLDPRS_RP FLU_RP PNEUM_RP MAMM_RP PAPS_RP PSA_RP COL_FOBT_RP COL_FOBT_RP_LT5 COL_SCOP_RP COL_SCOP_RP_LT5 dm_ftexam dm_bldtst_self dm_slfmgt_ed dm_drvs dm_hba1c dm_cntrld PHQ8 PHQ8_NR PHQ2 PHQ2_NR GAD2 GAD2_NR PHQ_DP_DIF PRB_SLEEP MED_BILS_COLLECT HC_CST_DELAY 
						TRB_GT_CARE GPS_SCR NOMD_CST MD_CARE_HLTH MD_TRVL_EASE SPC_ACCS_SAT QUAL_SAT INFO_SAT AFRD_RPLC_FOOD AFRD_EAT_HLTH NOAFRD_EAT_LESS NOAFRD_HUNGRY OWN_HOME SECT_EGHT HOME_VALU 
						LIVE_ALON N_HHLD CS_USC_XSEE_WGT SV_USC_XSEE_WGT TUP_SCR USOC USOC_PCP USOC_SPC SPC_SR PCP_SPC_COORD USOC_VS TEST_SR TEST_FLWP_SR NEED_HOME_SR HELP_HOME_SR RX_SR INFO_RX_SR HEAR_PRB HEAR_AID CS_MED_XSEE_WGT SV_MED_XSEE_WGT SR_ED_VS_IND SR_OP_VS_IND SR_MD_VS_IND SR_MD_PCP_VS_IND SR_MD_SPC_VS_IND LTC_days LTC_SNF_days HH_VS CU_HH_CLM CU_IP_ST CU_IP_DY CU_SNF_ST CU_SNF_DY CU_ED_VS CU_CLN_VS MCAID_REIMB RX_ADRD_COV_DYS RX_ADRD_PDC 
						RX_ANDP_COV_DYS RX_ANDP_PDC AD_DX_CCW AD_DX_PRYR AMI_DX CD_DX AFB_DX BRC_DX CRC_DX ENC_DX LUC_DX PRC_DX HF_DX CKD_DX DP_DX_CCW DP_DX_CCW_DT SPDP_DX_CCW DM_DX DSL_DX HTN_DX IH_DX ART_DX SRK_DX ANX_DX_CCW ANX_DX_CCW_DT SP_DX_CCW HIV_DX ID_DX_CCW PVD_DX ALC_DX DRG_DX AD_DX AD_DX_CNT DP_DX SPDP_DX ANX_DX ID_DX ID_DX_CNT AU_DX AU_DX_CNT CP_DX CP_DX_CNT AD DM HF IH CD DP	LIV_DX CAN_DX CCI_Score HCC_Score HCC_subst HCC_psych);
merge sample_&YEAR. HCC_&YEAR.;
by baseid;
if CL_YR = &YEAR.;

HCC_Score=0;
if mcbs_inst=0 and MCAID_ANY=0 and OREC_AGE_ONLY=1 and NEW_BENE=0 then HCC_Score=SCORE_COMMUNITY_NA; /* community model Non Dual aged */
if mcbs_inst=0 and MCAID_ANY=0 and OREC_AGE_ONLY=0 and NEW_BENE=0 then HCC_Score=SCORE_COMMUNITY_ND; /* community model Non Dual disabled */
if mcbs_inst=0 and MCAID_ANY=1 and OREC_AGE_ONLY=1 and NEW_BENE=0 then HCC_Score=SCORE_COMMUNITY_FBA; /* community model Dual aged */
if mcbs_inst=0 and MCAID_ANY=1 and OREC_AGE_ONLY=0 and NEW_BENE=0 then HCC_Score=SCORE_COMMUNITY_FBD; /* community model Dual disabled */
if mcbs_inst=1 then HCC_Score=SCORE_INSTITUTIONAL;													/* institutional model */
if NEW_BENE=1 then HCC_Score=SCORE_NEW_ENROLLEE;												/* new beneficiary model */

run;

/* 18 - Running the AHRQ PQI Algorithm */

/* getting needed variables from the inpatient data */																
Data inp_&YEAR. (keep = BASEID DX1 DX2 DX3 DX4 DX5 DX6 DX7 DX8 DX9 dx10 dx11 dx12 dx13 dx14 dx15 dx16 dx17 dx18 dx19 dx20 dx21 dx22 dx23 dx24 dx25 PR1 PR2 PR3 PR4 PR5 PR6 PR7
				PR8 PR9 PR10 PR11 PR12 PR13 PR14 PR15 PR16 PR17 PR18 PR19 PR20 PR21 PR22 PR23 PR24 PR25 POINTOFORIGINUB04 YEAR DQTR AGE DRG);
set claims&YR..inp;
dx1=ICD_DGNS_CD1; dx2=ICD_DGNS_CD2; dx3=ICD_DGNS_CD3; dx4=ICD_DGNS_CD4; dx5=ICD_DGNS_CD5; dx6=ICD_DGNS_CD6; dx7=ICD_DGNS_CD7; dx8=ICD_DGNS_CD8; dx9=ICD_DGNS_CD9; 
dx10=ICD_DGNS_CD10; dx11=ICD_DGNS_CD11; dx12=ICD_DGNS_CD12; dx13=ICD_DGNS_CD13; dx14=ICD_DGNS_CD14; dx15=ICD_DGNS_CD15; dx16=ICD_DGNS_CD16; dx17=ICD_DGNS_CD17; dx18=ICD_DGNS_CD18; 
dx19=ICD_DGNS_CD19; dx20=ICD_DGNS_CD20; dx21=ICD_DGNS_CD21; dx22=ICD_DGNS_CD22; dx23=ICD_DGNS_CD23; dx24=ICD_DGNS_CD24; dx25=ICD_DGNS_CD25; 	

c_dx1=put(dx1,$7.); drop dx1; rename c_dx1=dx1;
c_dx2=put(dx2,$7.); drop dx2; rename c_dx2=dx2;
c_dx3=put(dx3,$7.); drop dx3; rename c_dx3=dx3;
c_dx4=put(dx4,$7.); drop dx4; rename c_dx4=dx4;
c_dx5=put(dx5,$7.); drop dx5; rename c_dx5=dx5;
c_dx6=put(dx6,$7.); drop dx6; rename c_dx6=dx6;
c_dx7=put(dx7,$7.); drop dx7; rename c_dx7=dx7;
c_dx8=put(dx8,$7.); drop dx8; rename c_dx8=dx8;
c_dx9=put(dx9,$7.); drop dx9; rename c_dx9=dx9;	
c_dx10=put(dx10,$7.); drop dx10; rename c_dx10=dx10;	
c_dx11=put(dx11,$7.); drop dx11; rename c_dx11=dx11;
c_dx12=put(dx12,$7.); drop dx12; rename c_dx12=dx12;
c_dx13=put(dx13,$7.); drop dx13; rename c_dx13=dx13;
c_dx14=put(dx14,$7.); drop dx14; rename c_dx14=dx14;
c_dx15=put(dx15,$7.); drop dx15; rename c_dx15=dx15;
c_dx16=put(dx16,$7.); drop dx16; rename c_dx16=dx16;
c_dx17=put(dx17,$7.); drop dx17; rename c_dx17=dx17;
c_dx18=put(dx18,$7.); drop dx18; rename c_dx18=dx18;
c_dx19=put(dx19,$7.); drop dx19; rename c_dx19=dx19;
c_dx20=put(dx20,$7.); drop dx20; rename c_dx20=dx20;
c_dx21=put(dx21,$7.); drop dx21; rename c_dx21=dx21;
c_dx22=put(dx22,$7.); drop dx22; rename c_dx22=dx22;
c_dx23=put(dx23,$7.); drop dx23; rename c_dx23=dx23;
c_dx24=put(dx24,$7.); drop dx24; rename c_dx24=dx24;
c_dx25=put(dx25,$7.); drop dx25; rename c_dx25=dx25;

PR1=ICD_PRCDR_CD1; PR2=ICD_PRCDR_CD2; PR3=ICD_PRCDR_CD3; PR4=ICD_PRCDR_CD4; PR5=ICD_PRCDR_CD5; PR6=ICD_PRCDR_CD6; PR7=ICD_PRCDR_CD7; PR8=ICD_PRCDR_CD8; PR9=ICD_PRCDR_CD9;
PR10=ICD_PRCDR_CD10; PR11=ICD_PRCDR_CD11; PR12=ICD_PRCDR_CD12; PR13=ICD_PRCDR_CD13; PR14=ICD_PRCDR_CD14; PR15=ICD_PRCDR_CD15; PR16=ICD_PRCDR_CD16; PR17=ICD_PRCDR_CD17; 
PR18=ICD_PRCDR_CD18; PR19=ICD_PRCDR_CD19; PR20=ICD_PRCDR_CD20; PR21=ICD_PRCDR_CD21; PR22=ICD_PRCDR_CD22; PR23=ICD_PRCDR_CD23; PR24=ICD_PRCDR_CD24; PR25=ICD_PRCDR_CD25;

POINTOFORIGINUB04=CLM_SRC_IP_ADMSN_CD;
YEAR=YEAR(CLM_THRU_DT);
DQTR=QTR(CLM_THRU_DT);

DRG = input(CLM_DRG_CD, 3.);
DOB = DOB_DT; format DOB mmddyy10.; AGE = ROUND(((MDY(01,01,&YEAR.) - DOB)/365.25),0.01); 

run;

TITLE 'AHRQ PREVENTION QUALITY INDICATORS: ASSIGN QIS TO INPATIENT DATA';  
OPTIONS COMPRESS=YES;      
%LET INFILE0  = inp_&YEAR.;                
%LET OUTFILE1 = PQI_&YEAR.;                      	 
%LET NDX = 25;                                 
%LET NPR = 25;       
libname LIBRARY 'R:\Active\mcbs\SAS\data\temp';
FILENAME PQIFMTS 'R:\Active\mcbs\SAS\pqis\icd10\PQI_ALL_FORMATS.SAS'; 
%INCLUDE PQIFMTS;
DATA &OUTFILE1. (KEEP=BASEID YEAR DQTR TAPQ01--TAPQ16 TAPQ90-TAPQ93);
SET &INFILE0. (KEEP=BASEID YEAR DQTR POINTOFORIGINUB04 AGE DRG DX1-DX&NDX. PR1-PR&NPR.);
* -------------------------------------------------------------- ;
 * --- DIAGNOSIS AND PROCEDURE MACROS --------------------------- ;
 * -------------------------------------------------------------- ;
/*Macro to compare all discharge diagnosis codes against format.*/
%MACRO MDX(FMT);
 (%DO I = 1 %TO &NDX.-1;
  (put(DX&I.,&FMT.) = '1') OR
  %END;
  (put(DX&NDX.,&FMT.) = '1'))
 %MEND;

 /*Macro to compare discharge primary diagnosis code against format.*/
 %MACRO MDX1(FMT);
 ((put(DX1,&FMT.) = '1'))
 %MEND;

 /*Macro to compare discharge secondary diagnosis codes against format.*/
 %MACRO MDX2(FMT);
 (%DO I = 2 %TO &NDX.-1;
  (put(DX&I.,&FMT.) = '1') OR
  %END;
  (put(DX&NDX.,&FMT.) = '1'))
 %MEND;

  /*Macro to compare all discharge procedure codes against format.*/
 %MACRO MPR(FMT);
 (%DO I = 1 %TO &NPR.-1;
  (put(PR&I.,&FMT.) = '1') OR
  %END;
  (put(PR&NPR.,&FMT.) = '1'))
 %MEND;
 * ---------------------------------------------------------------- ;
 * --- DEFINE MDC ------------------------------------------------- ;
 * ---------------------------------------------------------------- ;
 attrib MDCNEW length=3
   label='RESPECIFIED MDC';

 if MDC notin (01,02,03,04,05,06,07,08,09,10,
               11,12,13,14,15,16,17,18,19,20,
               21,22,23,24,25)
 then do;
    MDCNEW = put(DRG,MDCF2T.);
    if MDCNEW in (01,02,03,04,05,06,07,08,09,10,
                  11,12,13,14,15,16,17,18,19,20,
                  21,22,23,24,25)
    then MDC=MDCNEW;
    else do;
        if DRG in (999) then MDC = 0;
       else put "INVALID MDC KEY: " KEY " MDC " MDC " DRG " DRG ;
    end;
 end;

  * -------------------------------------------------------------- ;
 * --- DELETE RECORDS WITH MISSING VALUES FOR AGE OR DX1-- ;
 * --- DELETE NON ADULT RECORDS                         --------- ;
 * -------------------------------------------------------------- ;
 if Age lt 0 then delete;
 if Age lt 18 and MDC notin (14) then delete;
 if DX1 in (' ') then delete;
 if DQTR le .Z then delete;
 if YEAR le .Z then delete;

* --------------------------------------------------------------- ;
 * --- DEFINE ICD-10-CM VERSION ---------------------------------- ;
 * --------------------------------------------------------------- ;
 attrib ICDVER length=3
   label='ICD-10-CM VERSION';

 ICDVER = 0;
 if (YEAR in (2015) and DQTR in (4))          then ICDVER = 33;
 else if (YEAR in (2016) and DQTR in (1,2,3)) then ICDVER = 33;
 else if (YEAR in (2016) and DQTR in (4))     then ICDVER = 34;
 else if (YEAR in (2017) and DQTR in (1,2,3)) then ICDVER = 34;
 else if (YEAR in (2017) and DQTR in (4))     then ICDVER = 35;
 else if (YEAR in (2018) and DQTR in (1,2,3)) then ICDVER = 35;
 else ICDVER = 35; *Defaults to last version for discharges outside coding updates.;

 * -------------------------------------------------------------- ;
 * --- DEFINE AREA LEVEL ACSC INDICATORS ------------------------ ;
 * -------------------------------------------------------------- ;
 length TAPQ01 TAPQ02 TAPQ03 TAPQ05
        TAPQ07 TAPQ08 TAPQ10 TAPQ11 
        TAPQ12 TAPQ14 TAPQ15
        TAPQ16 TAPQ90 TAPQ91 TAPQ92 TAPQ93 8;
 label
 TAPQ01 = 'PQI 01 Diabetes Short-Term Complications Admission Rate (Numerator)'
 TAPQ02 = 'PQI 02 Perforated Appendix Admission Rate (Numerator)'
 TAPQ03 = 'PQI 03 Diabetes Long-Term Complications Admission Rate (Numerator)'
 TAPQ05 = 'PQI 05 Chronic Obstructive Pulmonary Disease (COPD) or Asthma in Older Adults Admission Rate (Numerator)'
 TAPQ07 = 'PQI 07 Hypertension Admission Rate (Numerator)'
 TAPQ08 = 'PQI 08 Heart Failure Admission Rate (Numerator)'
 TAPQ10 = 'PQI 10 Dehydration Admission Rate (Numerator)'
 TAPQ11 = 'PQI 11 Community-Acquired Pneumonia Admission Rate (Numerator)'
 TAPQ12 = 'PQI 12 Urinary Tract Infection Admission Rate (Numerator)'
 TAPQ14 = 'PQI 14 Uncontrolled Diabetes Admission Rate (Numerator)'
 TAPQ15 = 'PQI 15 Asthma in Younger Adults Admission Rate (Numerator)'
 TAPQ16 = 'PQI 16 Lower-Extremity Amputation Among Patients with Diabetes Rate (Numerator)'
 TAPQ90 = 'PQI 90 Prevention Quality Overall Composite (Numerator)'
 TAPQ91 = 'PQI 91 Prevention Quality Acute Composite (Numerator)'
 TAPQ92 = 'PQI 92 Prevention Quality Chronic Composite (Numerator)'
 TAPQ93 = 'PQI 93 Prevention Quality Diabetes Composite (Numerator)'
;
* ------------------------------------------------------------------ ;
 * --- PQI 01 : DIABETES SHORT-TERM COMPLICATIONS ADMISSION RATE  --- ;
 * ------------------------------------------------------------------ ;
   if %MDX1($ACDIASD.) then
        TAPQ01 = 1;
 * -------------------------------------------------- ;
 * --- PQI 02 : PERFORATED APPENDIX ADMISSION RATE --- ;
 * -------------------------------------------------- ;
   if %MDX($ACSAP2D.) then do;
      TAPQ02 = 0;
      if %MDX($ACSAPPD.) then TAPQ02 = 1;
      *** Exclude: MDC 14;
      if MDC in (14) then TAPQ02 = .;
   end;
 * ----------------------------------------------------------------- ;
 * --- PQI 03 : DIABETES LONG-TERM COMPLICATIONS ADMISSION RATE  --- ;
 * ----------------------------------------------------------------- ;
   if %MDX1($ACDIALD.) then
        TAPQ03 = 1;
 * ------------------------------------------------------------------------------------------------------ ;
 * --- PQI 05 : CHRONIC OBSTRUCTIVE PULMONARY DISEASE (COPD) OR ASTHMA IN OLDER ADULTS ADMISSION RATE --- ;
 * ------------------------------------------------------------------------------------------------------ ;
   if AGE ge 40 then do;
     if %MDX1($ACCOPDD.) or %MDX1($ACSASTD.) 
        then do;  TAPQ05 = 1;
   *** Exclude Cystic Fibrosis and Anomalies 
          of the Respiratory System;
      if %MDX($RESPAN.) then TAPQ05 = .;
     end;
   end;
 * ------------------------------------------------ ;
 * --- PQI 07 : HYPERTENSION ADMISSION RATE     --- ;
 * ------------------------------------------------ ;
   if %MDX1($ACSHYPD.) then do;
        TAPQ07 = 1;
      ***Exclude Stage I-IV Kidney Disease
         with dialysis access procedures;
      if %MDX($ACSHY2D.) and %MPR($DIALY2P.)
      then TAPQ07 = .;
      *** Exclude Cardiac Procedures;
      if %MPR($ACSCARP.) then TAPQ07 = .;
   end;
 * -------------------------------------------------- ;
 * --- PQI 08 : HEART FAILURE ADMISSION RATE      --- ;
 * -------------------------------------------------- ;
   if %MDX1($MRTCHFD.) then do;
      TAPQ08 = 1;
      *** Exclude Cardiac Procedures;
      if %MPR($ACSCARP.) then TAPQ08 = .;
   end;
 * -------------------------------------------------- ;
 * --- PQI 10 : DEHYDRATION ADMISSION RATE        --- ;
 * -------------------------------------------------- ;
   if %MDX1($ACSDEHD.) or  
      (%MDX2($ACSDEHD.) and (%MDX1($HYPERID.) or %MDX1($ACPGASD.) or %MDX1($PHYSIDB.))) 
       then do;       TAPQ10 = 1;
         *** Exclude chronic renal failure ****;
         if %MDX($CRENLFD.) then TAPQ10 = .;
   end;
 * ------------------------------------------------------------ ;
 * --- PQI 11 : COMMUNITY-ACQUIRED PNEUMONIA ADMISSION RATE --- ;
 * ------------------------------------------------------------ ;
   if %MDX1($ACSBACD.) then do;
      TAPQ11 = 1;
      *** Exclude: Sickle Cell;
      if %MDX($ACSBA2D.)  then TAPQ11 = .;
      *** Exclude Immunocompromised state;
      if %MDX($IMMUNID.) or %MPR($IMMUNIP.) 
      then TAPQ11 = .;
   end;
 * ------------------------------------------------------- ;
 * --- PQI 12 : URINARY TRACK INFECTION ADMISSION RATE --- ;
 * ------------------------------------------------------- ;
   if  %MDX1($ACSUTID.) then do;
      TAPQ12 = 1;
      *** Exclude Immunocompromised state and 
          Kidney/Urinary Tract Disorder;
      if %MDX($IMMUNID.) or %MPR($IMMUNIP.) or 
         %MDX($KIDNEY.) 
      then TAPQ12 = .;
   end;
 * ----------------------------------------------------- ;
 * --- PQI 14 : UNCONTROLLED DIABETES ADMISSION RATE --- ;
 * ----------------------------------------------------- ;
   if %MDX1($ACDIAUD.) then
      TAPQ14 = 1;
 * ----------------------------------------------------------------- ;
 * --- PQI 15 : ASTHMA IN YOUNGER ADULTS DIABETES ADMISSION RATE --- ;
 * ----------------------------------------------------------------- ;
   if %MDX1($ACSASTD.) then do;
      TAPQ15 = 1;
      *** Exclude Cystic Fibrosis and Anomalies 
          of the Respiratory System;
      if %MDX($RESPAN.) then TAPQ15 = .;
      if AGE ge 40 then TAPQ15 = .;
  end;
 * ----------------------------------------------------------------------------- ;
 * --- PQI 16 : LOWER-EXTREMITY AMPUTATION AMONG PATIENTS WITH DIABETES RATE --- ;
 * ----------------------------------------------------------------------------- ;
   if %MPR($ACSLEAP.) and %MDX($ACSLEAD.) then do;
      TAPQ16 = 1;
      *** Exclude: MDC 14;
      if MDC in (14) then TAPQ16 = .;
      *** Exclude: Trauma;
      if %MDX($ACLEA2D.) then TAPQ16 = .;
   end;
 * -------------------------------------------------------------- ;
 * --- CONSTRUCT AREA LEVEL COMPOSITE INDICATORS ---------------- ;
 * -------------------------------------------------------------- ;
 * ----------------------------------------------------- ;
 * --- PQI 90 : PREVENTION QUALITY OVERALL COMPOSITE --- ;
 * ----------------------------------------------------- ;
   if TAPQ01 = 1 or TAPQ03 = 1 or TAPQ05 = 1 or TAPQ07 = 1 or 
      TAPQ08 = 1 or TAPQ10 = 1 or TAPQ11 = 1 or TAPQ12 = 1 or 
      TAPQ14 = 1 or TAPQ15 = 1 or TAPQ16 = 1
   then 
   TAPQ90 = MAX(OF TAPQ01 TAPQ03 TAPQ05 TAPQ07 
                   TAPQ08 TAPQ10 TAPQ11 TAPQ12
                   TAPQ14 TAPQ15 TAPQ16);
 * --------------------------------------------------- ;
 * --- PQI 91 : PREVENTION QUALITY ACUTE COMPOSITE --- ;
 * --------------------------------------------------- ;
   if TAPQ10 = 1 or TAPQ11 = 1 or TAPQ12 = 1 
   then 
   TAPQ91 = MAX(OF TAPQ10 TAPQ11 TAPQ12);
 * ----------------------------------------------------- ;
 * --- PQI 92 : PREVENTION QUALITY CHRONIC COMPOSITE --- ;
 * ----------------------------------------------------- ;
   if TAPQ01 = 1 or TAPQ03 = 1 or TAPQ05 = 1 or TAPQ07 = 1 or 
      TAPQ08 = 1 or TAPQ14 = 1 or TAPQ15 = 1 or 
      TAPQ16 = 1
   then 
   TAPQ92 = MAX(OF TAPQ01 TAPQ03 TAPQ05 TAPQ07 
                   TAPQ08 TAPQ14 TAPQ15
                   TAPQ16);
 * ------------------------------------------------------ ;
 * --- PQI 93 : PREVENTION QUALITY DIABETES COMPOSITE --- ;
 * ------------------------------------------------------ ;
   IF TAPQ01 = 1 OR TAPQ03 = 1 OR TAPQ14 = 1 OR TAPQ16 = 1
   THEN 
   TAPQ93 = MAX(OF TAPQ01 TAPQ03 TAPQ14 TAPQ16);
 * -------------------------------------------------------------- ;
 * --- EXCLUDE TRANSFERS ---------------------------------------- ;
 * -------------------------------------------------------------- ;
 * --- TRANSFER FROM ANOTHER ---------------- ;
 IF /*ASOURCE IN (2,3) OR*/ POINTOFORIGINUB04 IN ('4','5','6')
 THEN DO;
   TAPQ01 = .;
   TAPQ03 = .;
   TAPQ05 = .;
   TAPQ07 = .;
   TAPQ08 = .;
   TAPQ10 = .;
   TAPQ11 = .;
   TAPQ12 = .;
   TAPQ14 = .;
   TAPQ15 = .;
   TAPQ16 = .;
   TAPQ90 = .;
   TAPQ91 = .;
   TAPQ92 = .;
 END;
RUN;

PROC SORT DATA=&OUTFILE1.;
BY BASEID;
RUN;

PROC SUMMARY DATA=&OUTFILE1.;
BY BASEID;
VAR TAPQ01 TAPQ03 TAPQ05 TAPQ07 TAPQ08 TAPQ10 TAPQ11 TAPQ12 TAPQ14 TAPQ15 TAPQ16 TAPQ90 TAPQ91 TAPQ92 TAPQ93;
OUTPUT OUT=ACSC_&YEAR. (KEEP= BASEID TAPQ01 TAPQ03 TAPQ05 TAPQ07 TAPQ08 TAPQ10 TAPQ11 TAPQ12 TAPQ14 TAPQ15 TAPQ16 TAPQ90 TAPQ91 TAPQ92 TAPQ93)SUM=;
RUN;

DATA ACSC_&YEAR. (KEEP= BASEID DM_ACSHP CV_ACSHP ACSHP ACSHP_ACUT ACSHP_CHRN);
SET ACSC_&YEAR.;
IF TAPQ01 = . THEN TAPQ01 = 0;
IF TAPQ03 = . THEN TAPQ03 = 0;
IF TAPQ05 = . THEN TAPQ05 = 0;
IF TAPQ07 = . THEN TAPQ07 = 0;
IF TAPQ08 = . THEN TAPQ08 = 0;
IF TAPQ10 = . THEN TAPQ10 = 0;
IF TAPQ11 = . THEN TAPQ11 = 0;
IF TAPQ12 = . THEN TAPQ12 = 0;
IF TAPQ14 = . THEN TAPQ14 = 0;
if TAPQ15 =.  then TAPQ15 =0;	
IF TAPQ16 = . THEN TAPQ16 = 0;
IF TAPQ90 = . THEN TAPQ90 = 0;
IF TAPQ91 = . THEN TAPQ91 = 0;
IF TAPQ92 = . THEN TAPQ92 = 0;
IF TAPQ93 = . THEN TAPQ93 = 0;
DM_ACSHP = TAPQ93;
CV_ACSHP = TAPQ07 + TAPQ08;
ACSHP = TAPQ90;
ACSHP_ACUT = TAPQ91;
ACSHP_CHRN = TAPQ92;
RUN;

/* counting hospital stays and days */
data inp_sum_&YEAR. (keep = baseid CLM_THRU_DT ip_st IP_DY CV_ST CV_DY); 
set claims&YR..inp;
IP_ST=1;
IP_DY=(CLM_THRU_DT - CLM_FROM_DT) + 1;

if PRNCPAL_DGNS_CD in ('I050','I051','I052','I058','I059','I060','I061','I062','I068','I069','I070','I071','I072','I078','I079','I080','I081','I082','I083','I088','I089','I091',
'I0989','I340','I341','I342','I348','I349','I350','I351','I352','I358','I359','I360','I361','I362','I368','I369','I370','I371','I372','I378','I379','R008','R009','R010','R011','R012',
'Z952','Z953','Z954','A3681','A381','A3950','A3951','A3952','A3953','B2682','B3320','B3321','B3322','B3323','B3324','B376','B5881','I010','I011','I012','I018','I019','I020','I090',
'I092','I099','I230','I300','I301','I308','I309','I310','I311','I312','I313','I314','I318','I319','I32','I330','I339','I38','I39','I400','I401','I408','I409','I41','I420','I421',
'I422','I423','I424','I425','I427','I428','I429','I43','I514','J1082','I10','I110','I119','I120','I129','I130','I1310','I1311','I132','I150','I151','I152','I158','I159','I160','I161',
'I169','I674','N262','I2101','I2102','I2109','I2111','I2119','I2121','I2129','I213','I214','I219','I21A1','I21A9','I220','I221','I222','I228','I229','I200','I201','I208','I209',
'I237','I240','I241','I248','I249','I2510','I25110','I25111','I25118','I25119','I252','I255','I256','I25750','I25751','I25758','I25759','I25811','I2582','I2583','I2584','I2589',
'I259','Z951','Z955','Z9861','R072','R0782','R0789','R079','I2601','I2602','I2609','I2690','I2692','I2699','I270','I271','I272','I2720','I2721','I2722','I2723','I2724','I2729',
'I2781','I2782','I2783','I2789','I279','I280','I281','I288','I289','Z86711','I231','I232','I233','I234','I235','I236','I238','I253','I2541','I2542','I510','I511','I512','I513',
'I515','I517','I5181','I5189','I519','I52','I440','I441','I442','I4430','I4439','I444','I445','I4460','I4469','I447','I450','I4510','I4519','I452','I453','I454','I455','I456',
'I4581','I4589','I459','Z45010','Z45018','Z4502','Z4509','Z950','Z95810','I470','I471','I472','I479','I480','I481','I482','I483','I484','I4891','I4892','I491','I492','I493','I4940',
'I4949','I495','I498','I499','R000','R001','R002','I462','I468','I469','I4901','I4902','I0981','I501','I5020','I5021','I5022','I5023','I5030','I5031','I5032','I5033','I5040','I5041',
'I5042','I5043','I50810','I50811','I50812','I50813','I50814','I5082','I5083','I5084','I5089','I509','G43601','G43609','G43611','G43619','I6000','I6001','I6002','I6010','I6011',
'I6012','I602','I6020','I6021','I6022','I6030','I6031','I6032','I604','I6050','I6051','I6052','I606','I607','I608','I609','I610','I611','I612','I613','I614','I615','I616','I618',
'I619','I6200','I6201','I6202','I6203','I621','I629','I6300','I63011','I63012','I63013','I63019','I6302','I63031','I63032','I63033','I63039','I6309','I6310','I63111','I63112',
'I63113','I63119','I6312','I63131','I63132','I63133','I63139','I6319','I6320','I63211','I63212','I63213','I63219','I6322','I63231','I63232','I63233','I63239','I6329','I6330','I63311',
'I63312','I63313','I63319','I63321','I63322','I63323','I63329','I63331','I63332','I63333','I63339','I63341','I63342','I63343','I63349','I6339','I6340','I63411','I63412','I63413',
'I63419','I63421','I63422','I63423','I63429','I63431','I63432','I63433','I63439','I63441','I63442','I63443','I63449','I6349','I6350','I63511','I63512','I63513','I63519','I63521',
'I63522','I63523','I63529','I63531','I63532','I63533','I63539','I63541','I63542','I63543','I63549','I6359','I636','I638','I6381','I6389','I639','I6601','I6602','I6603','I6609',
'I6611','I6612','I6613','I6619','I6621','I6622','I6623','I6629','I663','I668','I669','R29700','R29701','R29702','R29703','R29704','R29705','R29706','R29707','R29708','R29709',
'R29710','R29711','R29712','R29713','R29714','R29715','R29716','R29717','R29718','R29719','R29720','R29721','R29722','R29723','R29724','R29725','R29726','R29727','R29728','R29729',
'R29730','R29731','R29732','R29733','R29734','R29735','R29736','R29737','R29738','R29739','R29740','R29741','R29742','I6501','I6502','I6503','I6509','I651','I6521','I6522','I6523',
'I6529','I658','I659','G460','G461','G462','G463','G464','G465','G466','G467','G468','I671','I672','I675','I676','I677','I6781','I6782','I6783','I67841','I67848','I67850','I67858',
'I6789','I679','I680','I682','I688','G450','G451','G452','G453','G454','G458','G459','I6900','I6901','I69010','I69011','I69012','I69013','I69014','I69015','I69018','I69019','I69020',
'I69021','I69022','I69023','I69028','I69031','I69032','I69033','I69034','I69039','I69041','I69042','I69043','I69044','I69049','I69051','I69052','I69053','I69054','I69059','I69061',
'I69062','I69063','I69064','I69065','I69069','I69090','I69091','I69092','I69093','I69098','I6910','I6911','I69110','I69111','I69112','I69113','I69114','I69115','I69118','I69119',
'I69120','I69121','I69122','I69123','I69128','I69131','I69132','I69133','I69134','I69139','I69141','I69142','I69143','I69144','I69149','I69151','I69152','I69153','I69154','I69159',
'I69161','I69162','I69163','I69164','I69165','I69169','I69190','I69191','I69192','I69193','I69198','I6920','I6921','I69210','I69211','I69212','I69213','I69214','I69215','I69218',
'I69219','I69220','I69221','I69222','I69223','I69228','I69231','I69232','I69233','I69234','I69239','I69241','I69242','I69243','I69244','I69249','I69251','I69252','I69253','I69254',
'I69259','I69261','I69262','I69263','I69264','I69265','I69269','I69290','I69291','I69292','I69293','I69298','I6930','I6931','I69310','I69311','I69312','I69313','I69314','I69315',
'I69318','I69319','I69320','I69321','I69322','I69323','I69328','I69331','I69332','I69333','I69334','I69339','I69341','I69342','I69343','I69344','I69349','I69351','I69352','I69353',
'I69354','I69359','I69361','I69362','I69363','I69364','I69365','I69369','I69390','I69391','I69392','I69393','I69398','I6980','I6981','I69810','I69811','I69812','I69813','I69814',
'I69815','I69818','I69819','I69820','I69821','I69822','I69823','I69828','I69831','I69832','I69833','I69834','I69839','I69841','I69842','I69843','I69844','I69849','I69851','I69852',
'I69853','I69854','I69859','I69861','I69862','I69863','I69864','I69865','I69869','I69890','I69891','I69892','I69893','I69898','I6990','I6991','I69910','I69911','I69912','I69913',
'I69914','I69915','I69918','I69919','I69920','I69921','I69922','I69923','I69928','I69931','I69932','I69933','I69934','I69939','I69941','I69942','I69943','I69944','I69949','I69951',
'I69952','I69953','I69954','I69959','I69961','I69962','I69963','I69964','I69965','I69969','I69990','I69991','I69992','I69993','I69998','I700','I701','I70201','I70202','I70203',
'I70208','I70209','I70211','I70212','I70213','I70218','I70219','I70221','I70222','I70223','I70228','I70229','I70231','I70232','I70233','I70234','I70235','I70238','I70239','I70241',
'I70242','I70243','I70244','I70245','I70248','I70249','I7025','I70291','I70292','I70293','I70298','I70299','I708','I7090','I7091','I7092','I739','K550','K55011','K55012','K55019',
'K55021','K55022','K55029','K55031','K55032','K55039','K55041','K55042','K55049','K55051','K55052','K55059','K55061','K55062','K55069','K551','K558','K559','I670','I7100','I7101',
'I7102','I7103','I711','I712','I713','I714','I715','I716','I718','I719','I720','I721','I722','I723','I724','I725','I726','I728','I729','I7770','I7771','I7772','I7773','I7774','I7775',
'I7776','I7777','I7779','I77810','I77811','I77812','I77819','I790','I7401','I7409','I7410','I7411','I7419','I742','I743','I744','I745','I748','I749','I75011','I75012','I75013',
'I75019','I75021','I75022','I75023','I75029','I7581','I7589','I731','I7381','I7389','I770','I771','I772','I773','I774','I775','I776','I7789','I779','I788','I789','I791','I798','I950',
'I951','I9589','I959','I998','I999','M310','R030','R031','R0989','R58','Z8673','Z8674','Z8679','Z941','Z943','Z95811','Z95812','Z95818','Z95820','Z95828','Z959','Z9862','I8000',
'I8001','I8002','I8003','I8010','I8011','I8012','I8013','I80201','I80202','I80203','I80209','I80211','I80212','I80213','I80219','I80221','I80222','I80223','I80229','I80231','I80232',
'I80233','I80239','I80291','I80292','I80293','I80299','I803','I808','I809','I821','I82210','I82211','I82220','I82221','I82290','I82291','I823','I82401','I82402','I82403','I82409',
'I82411','I82412','I82413','I82419','I82421','I82422','I82423','I82429','I82431','I82432','I82433','I82439','I82441','I82442','I82443','I82449','I82491','I82492','I82493','I82499',
'I824Y1','I824Y2','I824Y3','I824Y9','I824Z1','I824Z2','I824Z3','I824Z9','I82501','I82502','I82503','I82509','I82511','I82512','I82513','I82519','I82521','I82522','I82523','I82529',
'I82531','I82532','I82533','I82539','I82541','I82542','I82543','I82549','I82591','I82592','I82593','I82599','I825Y1','I825Y2','I825Y3','I825Y9','I825Z1','I825Z2','I825Z3','I825Z9',
'I82601','I82602','I82603','I82609','I82611','I82612','I82613','I82619','I82621','I82622','I82623','I82629','I82701','I82702','I82703','I82709','I82711','I82712','I82713','I82719',
'I82721','I82722','I82723','I82729','I82811','I82812','I82813','I82819','I82890','I82891','I8290','I8291','I82A11','I82A12','I82A13','I82A19','I82A21','I82A22','I82A23','I82A29',
'I82B11','I82B12','I82B13','I82B19','I82B21','I82B22','I82B23','I82B29','I82C11','I82C12','I82C13','I82C19','I82C21','I82C22','I82C23','I82C29','Z86718','I87001','I87002','I87003',
'I87009','I87011','I87012','I87013','I87019','I87021','I87022','I87023','I87029','I87031','I87032','I87033','I87039','I87091','I87092','I87093','I87099','I871','I872','I87301',
'I87302','I87303','I87309','I87311','I87312','I87313','I87319','I87321','I87322','I87323','I87329','I87331','I87332','I87333','I87339','I87391','I87392','I87393','I87399','I878',
'I879','I890')
then CV_ST=1;
if CV_ST=1 then CV_DY=(CLM_THRU_DT - CLM_FROM_DT) + 1;

run;

proc sort data = inp_sum_&YEAR. nodupkey; by baseid CLM_THRU_DT; run; 

proc summary data = inp_sum_&YEAR.;
by baseid;
var ip_st IP_DY CV_ST CV_DY;
output out= inp_sum_&YEAR.(keep = baseid ip_st IP_DY CV_ST CV_DY)sum=;
run;

Data sample_&YEAR. (keep = BASEID CL_YR PTA_MM PTB_MM FFS_MM MA_MM MA_FLAG PRVSUP PRVSUP_MED PRVSUP_RX VA_FLAG H_MEDSTA NEW_BENE ESRD DISABLED OREC_AGE_ONLY MCAID_ANY MCAID_FULL MCAID_PART MC_ENT_ST MCR_ENT_YR MCR_ENT_MM SHSV_ACO1 NXGN_ACO CPCPL_PCMH PTD_MM 
						PD_FLAG PARTD_LIS HASPTD HASPTB HASHMO MA_INS_NM PTC_STR_RT MA_SNP MA_SNP_CD MA_SNP_DE MA_SNP_FIDE MA_SNP_IN H_DOB DOB AGE_0101 DIED DOD_YRMNDY AGE64 AGE74 AGE84 AGE85 USRD MBRZP MBRCTY MBRST SSA_SCTY RUCA1 ruca_metro1 ruca_micro1
						ruca_rural1 ADI_SCR NEWENGLAND MIDWEST WESTNORTHCENTRAL SOUTHATLANTIC EASTSCENTRAL WESTSCENTRAL MOUNTAIN PACIFIC MALE FEMALE WHITE BLACK HISPANIC ASIAN NTVAM HAWPAC MIXED OTHER 
						OTHR_LANG ENG_SPK_PRB EDUNKN EDNOHS EDHSGD EDSMCL EDCLGD INCOME FPL_LTE100 FPL_101_200 FPL_GT200 MARRIED WIDOW DIV_SEP NEV_MARRY VET_STS WORKING_SR MCBS_INST MDS3_INST NE MW SO WE HSPC 
						SNF_ST SNF_DY PTA_REIMB PTB_REIMB TOT_REIMB PTD_REIMB SHSV_ACO SV_VARSTRATUM SV_VARPSU SV_XSEE_WGT CS_VARSTRATUM CS_VARPSU CS_XSEE_WGT ALZDMT_SR DPRSSN_SR
						DM_RP DM_T1 DM_T2 DM_PRE HF_RP IH_RP ANGINA_RP CD_RP HYST_RP CLN_CNCR_RP IDD_RP PSYCH_RP ADL IADL COGIMP_SR DF_LIFT_RP DF_KNEL_RP DF_WALK_RP DF_DRES_RP DF_EAT_RP OVRWT OBESE HT_CM WT_KG BAD_SR_HLTH WRS_SR_HLTH 
						TBC_NOW PREV_SMKR ALC_PRB BLDCHL_RP BLDPRS_RP FLU_RP PNEUM_RP MAMM_RP PAPS_RP PSA_RP COL_FOBT_RP COL_FOBT_RP_LT5 COL_SCOP_RP COL_SCOP_RP_LT5 dm_ftexam dm_bldtst_self dm_slfmgt_ed dm_drvs dm_hba1c dm_cntrld PHQ8 PHQ8_NR PHQ2 PHQ2_NR GAD2 GAD2_NR PHQ_DP_DIF PRB_SLEEP MED_BILS_COLLECT HC_CST_DELAY 
						TRB_GT_CARE GPS_SCR NOMD_CST MD_CARE_HLTH MD_TRVL_EASE SPC_ACCS_SAT QUAL_SAT INFO_SAT AFRD_RPLC_FOOD AFRD_EAT_HLTH NOAFRD_EAT_LESS NOAFRD_HUNGRY OWN_HOME SECT_EGHT HOME_VALU 
						LIVE_ALON N_HHLD CS_USC_XSEE_WGT SV_USC_XSEE_WGT TUP_SCR USOC USOC_PCP USOC_SPC SPC_SR PCP_SPC_COORD USOC_VS TEST_SR TEST_FLWP_SR NEED_HOME_SR HELP_HOME_SR RX_SR INFO_RX_SR HEAR_PRB HEAR_AID CS_MED_XSEE_WGT SV_MED_XSEE_WGT SR_ED_VS_IND SR_OP_VS_IND SR_MD_VS_IND SR_MD_PCP_VS_IND SR_MD_SPC_VS_IND LTC_days LTC_SNF_days HH_VS CU_HH_CLM CU_IP_ST CU_IP_DY CU_SNF_ST CU_SNF_DY CU_ED_VS CU_CLN_VS MCAID_REIMB RX_ADRD_COV_DYS RX_ADRD_PDC 
						RX_ANDP_COV_DYS RX_ANDP_PDC AD_DX_CCW AD_DX_PRYR AMI_DX CD_DX AFB_DX BRC_DX CRC_DX ENC_DX LUC_DX PRC_DX HF_DX CKD_DX DP_DX_CCW DP_DX_CCW_DT SPDP_DX_CCW DM_DX DSL_DX HTN_DX IH_DX ART_DX SRK_DX ANX_DX_CCW ANX_DX_CCW_DT SP_DX_CCW HIV_DX ID_DX_CCW PVD_DX ALC_DX DRG_DX AD_DX AD_DX_CNT DP_DX SPDP_DX ANX_DX ID_DX ID_DX_CNT AU_DX AU_DX_CNT CP_DX CP_DX_CNT AD DM HF IH CD DP	LIV_DX CAN_DX CCI_Score HCC_Score HCC_subst HCC_psych IP_ST IP_DY CV_ST 
						CV_DY DM_ACSHP CV_ACSHP ACSHP ACSHP_ACUT ACSHP_CHRN);
merge sample_&YEAR. inp_sum_&YEAR. ACSC_&YEAR.;
by baseid;
if CL_YR = &YEAR.;
if IP_ST=. then IP_ST=0;
if IP_DY=. then IP_DY=0;
if CV_ST=. then CV_ST=0;
if CV_DY=. then CV_DY=0;
if DM_ACSHP =. then DM_ACSHP =0;
if CV_ACSHP =. then CV_ACSHP =0;
if ACSHP_ACUT =. then ACSHP_ACUT =0;
if ACSHP_CHRN =. then ACSHP_CHRN =0;
if ACSHP =. then ACSHP =0;
run;

/* 19 - identifying total # of ED visits and running the NYU ED visit algorithm */

/* Setting up input dataset for ed visits */
Data otp_&YEAR. (keep = BASEID CLAIMID CLM_FROM_DT POS DX1 DX2 DX3 DX4 DX5 DX6 DX7 DX8 DX9 dx10 dx11 dx12 dx13 dx14 dx15 dx16 dx17 dx18 dx19 dx20 dx21 dx22 dx23 dx24 dx25 PR1 PR2 PR3 
						PR4 PR5 PR6 PR7 PR8 PR9 PR10 PR11 PR12 PR13 PR14 PR15 PR16 PR17 PR18 PR19 PR20 PR21 PR22 PR23 PR24 PR25 YEAR DQTR AGE);
set claims&YR..otp;
POS='ED';

dx1=ICD_DGNS_CD1; dx2=ICD_DGNS_CD2; dx3=ICD_DGNS_CD3; dx4=ICD_DGNS_CD4; dx5=ICD_DGNS_CD5; dx6=ICD_DGNS_CD6; dx7=ICD_DGNS_CD7; dx8=ICD_DGNS_CD8; dx9=ICD_DGNS_CD9; 
dx10=ICD_DGNS_CD10; dx11=ICD_DGNS_CD11; dx12=ICD_DGNS_CD12; dx13=ICD_DGNS_CD13; dx14=ICD_DGNS_CD14; dx15=ICD_DGNS_CD15; dx16=ICD_DGNS_CD16; dx17=ICD_DGNS_CD17; dx18=ICD_DGNS_CD18; 
dx19=ICD_DGNS_CD19; dx20=ICD_DGNS_CD20; dx21=ICD_DGNS_CD21; dx22=ICD_DGNS_CD22; dx23=ICD_DGNS_CD23; dx24=ICD_DGNS_CD24; dx25=ICD_DGNS_CD25; 	

c_dx1=put(dx1,$7.); drop dx1; rename c_dx1=dx1;
c_dx2=put(dx2,$7.); drop dx2; rename c_dx2=dx2;
c_dx3=put(dx3,$7.); drop dx3; rename c_dx3=dx3;
c_dx4=put(dx4,$7.); drop dx4; rename c_dx4=dx4;
c_dx5=put(dx5,$7.); drop dx5; rename c_dx5=dx5;
c_dx6=put(dx6,$7.); drop dx6; rename c_dx6=dx6;
c_dx7=put(dx7,$7.); drop dx7; rename c_dx7=dx7;
c_dx8=put(dx8,$7.); drop dx8; rename c_dx8=dx8;
c_dx9=put(dx9,$7.); drop dx9; rename c_dx9=dx9;	
c_dx10=put(dx10,$7.); drop dx10; rename c_dx10=dx10;	
c_dx11=put(dx11,$7.); drop dx11; rename c_dx11=dx11;
c_dx12=put(dx12,$7.); drop dx12; rename c_dx12=dx12;
c_dx13=put(dx13,$7.); drop dx13; rename c_dx13=dx13;
c_dx14=put(dx14,$7.); drop dx14; rename c_dx14=dx14;
c_dx15=put(dx15,$7.); drop dx15; rename c_dx15=dx15;
c_dx16=put(dx16,$7.); drop dx16; rename c_dx16=dx16;
c_dx17=put(dx17,$7.); drop dx17; rename c_dx17=dx17;
c_dx18=put(dx18,$7.); drop dx18; rename c_dx18=dx18;
c_dx19=put(dx19,$7.); drop dx19; rename c_dx19=dx19;
c_dx20=put(dx20,$7.); drop dx20; rename c_dx20=dx20;
c_dx21=put(dx21,$7.); drop dx21; rename c_dx21=dx21;
c_dx22=put(dx22,$7.); drop dx22; rename c_dx22=dx22;
c_dx23=put(dx23,$7.); drop dx23; rename c_dx23=dx23;
c_dx24=put(dx24,$7.); drop dx24; rename c_dx24=dx24;
c_dx25=put(dx25,$7.); drop dx25; rename c_dx25=dx25;

PR1=ICD_PRCDR_CD1; PR2=ICD_PRCDR_CD2; PR3=ICD_PRCDR_CD3; PR4=ICD_PRCDR_CD4; PR5=ICD_PRCDR_CD5; PR6=ICD_PRCDR_CD6; PR7=ICD_PRCDR_CD7; PR8=ICD_PRCDR_CD8; PR9=ICD_PRCDR_CD9;
PR10=ICD_PRCDR_CD10; PR11=ICD_PRCDR_CD11; PR12=ICD_PRCDR_CD12; PR13=ICD_PRCDR_CD13; PR14=ICD_PRCDR_CD14; PR15=ICD_PRCDR_CD15; PR16=ICD_PRCDR_CD16; PR17=ICD_PRCDR_CD17; 
PR18=ICD_PRCDR_CD18; PR19=ICD_PRCDR_CD19; PR20=ICD_PRCDR_CD20; PR21=ICD_PRCDR_CD21; PR22=ICD_PRCDR_CD22; PR23=ICD_PRCDR_CD23; PR24=ICD_PRCDR_CD24; PR25=ICD_PRCDR_CD25;

YEAR=YEAR(CLM_THRU_DT);
DQTR=QTR(CLM_THRU_DT);
DOB = DOB_DT; format DOB mmddyy10.; AGE = ROUND(((MDY(01,01,&YEAR.) - DOB)/365.25),0.01); 
run;

proc sort data = otp_&YEAR.; 
by BASEID CLAIMID; 
run;

proc sort data = claims&YR..otprev; 
by BASEID CLAIMID; 
run;

data otp_ed_&YEAR. (keep = BASEID CLAIMID CLM_FROM_DT DX1 DX2 DX3 DX4 DX5 DX6 DX7 DX8 DX9 dx10 dx11 dx12 dx13 dx14 dx15 dx16 dx17 dx18 dx19 dx20 dx21 dx22 dx23 dx24 dx25 PR1 PR2 PR3 
						   PR4 PR5 PR6 PR7 PR8 PR9 PR10 PR11 PR12 PR13 PR14 PR15 PR16 PR17 PR18 PR19 PR20 PR21 PR22 PR23 PR24 PR25 YEAR DQTR AGE);
merge otp_&YEAR. claims&YR..otprev;
by BASEID CLAIMID;
if POS='ED'; /*member is in the otp file above */
if REV_CNTR in ('0981','0450','0451','0452','0453','0454','0455','0456','0457','0458','0459'); /* ed visit identifier */
run;

Data inp_&YEAR. (keep = BASEID CLAIMID CLM_FROM_DT POS DX1 DX2 DX3 DX4 DX5 DX6 DX7 DX8 DX9 dx10 dx11 dx12 dx13 dx14 dx15 dx16 dx17 dx18 dx19 dx20 dx21 dx22 dx23 dx24 dx25 PR1 PR2 PR3 
						PR4 PR5 PR6 PR7 PR8 PR9 PR10 PR11 PR12 PR13 PR14 PR15 PR16 PR17 PR18 PR19 PR20 PR21 PR22 PR23 PR24 PR25 YEAR DQTR AGE);
set claims&YR..inp;
POS='ED';

dx1=ICD_DGNS_CD1; dx2=ICD_DGNS_CD2; dx3=ICD_DGNS_CD3; dx4=ICD_DGNS_CD4; dx5=ICD_DGNS_CD5; dx6=ICD_DGNS_CD6; dx7=ICD_DGNS_CD7; dx8=ICD_DGNS_CD8; dx9=ICD_DGNS_CD9; 
dx10=ICD_DGNS_CD10; dx11=ICD_DGNS_CD11; dx12=ICD_DGNS_CD12; dx13=ICD_DGNS_CD13; dx14=ICD_DGNS_CD14; dx15=ICD_DGNS_CD15; dx16=ICD_DGNS_CD16; dx17=ICD_DGNS_CD17; dx18=ICD_DGNS_CD18; 
dx19=ICD_DGNS_CD19; dx20=ICD_DGNS_CD20; dx21=ICD_DGNS_CD21; dx22=ICD_DGNS_CD22; dx23=ICD_DGNS_CD23; dx24=ICD_DGNS_CD24; dx25=ICD_DGNS_CD25; 	

c_dx1=put(dx1,$7.); drop dx1; rename c_dx1=dx1;
c_dx2=put(dx2,$7.); drop dx2; rename c_dx2=dx2;
c_dx3=put(dx3,$7.); drop dx3; rename c_dx3=dx3;
c_dx4=put(dx4,$7.); drop dx4; rename c_dx4=dx4;
c_dx5=put(dx5,$7.); drop dx5; rename c_dx5=dx5;
c_dx6=put(dx6,$7.); drop dx6; rename c_dx6=dx6;
c_dx7=put(dx7,$7.); drop dx7; rename c_dx7=dx7;
c_dx8=put(dx8,$7.); drop dx8; rename c_dx8=dx8;
c_dx9=put(dx9,$7.); drop dx9; rename c_dx9=dx9;	
c_dx10=put(dx10,$7.); drop dx10; rename c_dx10=dx10;	
c_dx11=put(dx11,$7.); drop dx11; rename c_dx11=dx11;
c_dx12=put(dx12,$7.); drop dx12; rename c_dx12=dx12;
c_dx13=put(dx13,$7.); drop dx13; rename c_dx13=dx13;
c_dx14=put(dx14,$7.); drop dx14; rename c_dx14=dx14;
c_dx15=put(dx15,$7.); drop dx15; rename c_dx15=dx15;
c_dx16=put(dx16,$7.); drop dx16; rename c_dx16=dx16;
c_dx17=put(dx17,$7.); drop dx17; rename c_dx17=dx17;
c_dx18=put(dx18,$7.); drop dx18; rename c_dx18=dx18;
c_dx19=put(dx19,$7.); drop dx19; rename c_dx19=dx19;
c_dx20=put(dx20,$7.); drop dx20; rename c_dx20=dx20;
c_dx21=put(dx21,$7.); drop dx21; rename c_dx21=dx21;
c_dx22=put(dx22,$7.); drop dx22; rename c_dx22=dx22;
c_dx23=put(dx23,$7.); drop dx23; rename c_dx23=dx23;
c_dx24=put(dx24,$7.); drop dx24; rename c_dx24=dx24;
c_dx25=put(dx25,$7.); drop dx25; rename c_dx25=dx25;

PR1=ICD_PRCDR_CD1; PR2=ICD_PRCDR_CD2; PR3=ICD_PRCDR_CD3; PR4=ICD_PRCDR_CD4; PR5=ICD_PRCDR_CD5; PR6=ICD_PRCDR_CD6; PR7=ICD_PRCDR_CD7; PR8=ICD_PRCDR_CD8; PR9=ICD_PRCDR_CD9;
PR10=ICD_PRCDR_CD10; PR11=ICD_PRCDR_CD11; PR12=ICD_PRCDR_CD12; PR13=ICD_PRCDR_CD13; PR14=ICD_PRCDR_CD14; PR15=ICD_PRCDR_CD15; PR16=ICD_PRCDR_CD16; PR17=ICD_PRCDR_CD17; 
PR18=ICD_PRCDR_CD18; PR19=ICD_PRCDR_CD19; PR20=ICD_PRCDR_CD20; PR21=ICD_PRCDR_CD21; PR22=ICD_PRCDR_CD22; PR23=ICD_PRCDR_CD23; PR24=ICD_PRCDR_CD24; PR25=ICD_PRCDR_CD25;

YEAR=YEAR(CLM_THRU_DT);
DQTR=QTR(CLM_THRU_DT);
DOB = DOB_DT; format DOB mmddyy10.; AGE = ROUND(((MDY(01,01,&YEAR.) - DOB)/365.25),0.01); 
run;

proc sort data = inp_&YEAR.; 
by BASEID CLAIMID; 
run;

proc sort data = claims&YR..inprev; 
by BASEID CLAIMID; 
run;

data inp_ed_&YEAR. (keep = BASEID CLAIMID CLM_FROM_DT DX1 DX2 DX3 DX4 DX5 DX6 DX7 DX8 DX9 dx10 dx11 dx12 dx13 dx14 dx15 dx16 dx17 dx18 dx19 dx20 dx21 dx22 dx23 dx24 dx25 PR1 PR2 PR3 
						   PR4 PR5 PR6 PR7 PR8 PR9 PR10 PR11 PR12 PR13 PR14 PR15 PR16 PR17 PR18 PR19 PR20 PR21 PR22 PR23 PR24 PR25 YEAR DQTR AGE);
merge inp_&YEAR. claims&YR..inprev;
by BASEID CLAIMID;
if POS='ED'; /*member is in the inp file above */
if REV_CNTR in ('0981','0450','0451','0452','0453','0454','0455','0456','0457','0458','0459'); /* ed visit identifier */
run;

Data temp.ed_&YEAR. (keep = BASEID CLAIMID CLM_FROM_DT DX1 DX2 DX3 DX4 DX5 DX6 DX7 DX8 DX9 dx10 dx11 dx12 dx13 dx14 dx15 dx16 dx17 dx18 dx19 dx20 dx21 dx22 dx23 dx24 dx25 PR1 PR2 PR3 
						       PR4 PR5 PR6 PR7 PR8 PR9 PR10 PR11 PR12 PR13 PR14 PR15 PR16 PR17 PR18 PR19 PR20 PR21 PR22 PR23 PR24 PR25 YEAR DQTR AGE ED_VS);
set otp_ed_&YEAR. inp_ed_&YEAR.;
ED_VS=1;
run;

proc sort data = temp.ed_&YEAR. nodupkey; /* deleting duplicate claims on the same date with the same principal discharge diagnosis */
by BASEID CLAIMID CLM_FROM_DT DX1; 
run;

/* counting ACSC related ED visits */

TITLE 'AHRQ PREVENTION QUALITY INDICATORS: ASSIGN QIS TO INPATIENT DATA';  
OPTIONS COMPRESS=YES;      
%LET INFILE0  = temp.ed_&YEAR.;                
%LET OUTFILE1 = edPQI_&YEAR.;                      	 
%LET NDX = 25;                                 
%LET NPR = 25;       
libname LIBRARY 'R:\Active\mcbs\SAS\data\temp';
FILENAME PQIFMTS 'R:\Active\mcbs\SAS\pqis\icd10\PQI_ALL_FORMATS.SAS'; 
%INCLUDE PQIFMTS;
DATA &OUTFILE1. (KEEP=BASEID YEAR DQTR TAPQ01--TAPQ16 TAPQ90-TAPQ93);
SET &INFILE0.;
* -------------------------------------------------------------- ;
 * --- DIAGNOSIS AND PROCEDURE MACROS --------------------------- ;
 * -------------------------------------------------------------- ;
/*Macro to compare all discharge diagnosis codes against format.*/
%MACRO MDX(FMT);
 (%DO I = 1 %TO &NDX.-1;
  (put(DX&I.,&FMT.) = '1') OR
  %END;
  (put(DX&NDX.,&FMT.) = '1'))
 %MEND;

 /*Macro to compare discharge primary diagnosis code against format.*/
 %MACRO MDX1(FMT);
 ((put(DX1,&FMT.) = '1'))
 %MEND;

 /*Macro to compare discharge secondary diagnosis codes against format.*/
 %MACRO MDX2(FMT);
 (%DO I = 2 %TO &NDX.-1;
  (put(DX&I.,&FMT.) = '1') OR
  %END;
  (put(DX&NDX.,&FMT.) = '1'))
 %MEND;

  /*Macro to compare all discharge procedure codes against format.*/
 %MACRO MPR(FMT);
 (%DO I = 1 %TO &NPR.-1;
  (put(PR&I.,&FMT.) = '1') OR
  %END;
  (put(PR&NPR.,&FMT.) = '1'))
 %MEND;

  * -------------------------------------------------------------- ;
 * --- DELETE RECORDS WITH MISSING VALUES FOR AGE OR DX1-- ;
 * --- DELETE NON ADULT RECORDS                         --------- ;
 * -------------------------------------------------------------- ;
 if DX1 in (' ') then delete;
 if DQTR le .Z then delete;
 if YEAR le .Z then delete;

* --------------------------------------------------------------- ;
 * --- DEFINE ICD-10-CM VERSION ---------------------------------- ;
 * --------------------------------------------------------------- ;
 attrib ICDVER length=3
   label='ICD-10-CM VERSION';

 ICDVER = 0;
 if (YEAR in (2015) and DQTR in (4))          then ICDVER = 33;
 else if (YEAR in (2016) and DQTR in (1,2,3)) then ICDVER = 33;
 else if (YEAR in (2016) and DQTR in (4))     then ICDVER = 34;
 else if (YEAR in (2017) and DQTR in (1,2,3)) then ICDVER = 34;
 else if (YEAR in (2017) and DQTR in (4))     then ICDVER = 35;
 else if (YEAR in (2018) and DQTR in (1,2,3)) then ICDVER = 35;
 else ICDVER = 35; *Defaults to last version for discharges outside coding updates.;

 * -------------------------------------------------------------- ;
 * --- DEFINE AREA LEVEL ACSC INDICATORS ------------------------ ;
 * -------------------------------------------------------------- ;
 length TAPQ01 TAPQ02 TAPQ03 TAPQ05
        TAPQ07 TAPQ08 TAPQ10 TAPQ11 
        TAPQ12 TAPQ14 TAPQ15
        TAPQ16 TAPQ90 TAPQ91 TAPQ92 TAPQ93 8;
 label
 TAPQ01 = 'PQI 01 Diabetes Short-Term Complications Admission Rate (Numerator)'
 TAPQ02 = 'PQI 02 Perforated Appendix Admission Rate (Numerator)'
 TAPQ03 = 'PQI 03 Diabetes Long-Term Complications Admission Rate (Numerator)'
 TAPQ05 = 'PQI 05 Chronic Obstructive Pulmonary Disease (COPD) or Asthma in Older Adults Admission Rate (Numerator)'
 TAPQ07 = 'PQI 07 Hypertension Admission Rate (Numerator)'
 TAPQ08 = 'PQI 08 Heart Failure Admission Rate (Numerator)'
 TAPQ10 = 'PQI 10 Dehydration Admission Rate (Numerator)'
 TAPQ11 = 'PQI 11 Community-Acquired Pneumonia Admission Rate (Numerator)'
 TAPQ12 = 'PQI 12 Urinary Tract Infection Admission Rate (Numerator)'
 TAPQ14 = 'PQI 14 Uncontrolled Diabetes Admission Rate (Numerator)'
 TAPQ15 = 'PQI 15 Asthma in Younger Adults Admission Rate (Numerator)'
 TAPQ16 = 'PQI 16 Lower-Extremity Amputation Among Patients with Diabetes Rate (Numerator)'
 TAPQ90 = 'PQI 90 Prevention Quality Overall Composite (Numerator)'
 TAPQ91 = 'PQI 91 Prevention Quality Acute Composite (Numerator)'
 TAPQ92 = 'PQI 92 Prevention Quality Chronic Composite (Numerator)'
 TAPQ93 = 'PQI 93 Prevention Quality Diabetes Composite (Numerator)'
;
* ------------------------------------------------------------------ ;
 * --- PQI 01 : DIABETES SHORT-TERM COMPLICATIONS ADMISSION RATE  --- ;
 * ------------------------------------------------------------------ ;
   if %MDX1($ACDIASD.) then
        TAPQ01 = 1;
 * -------------------------------------------------- ;
 * --- PQI 02 : PERFORATED APPENDIX ADMISSION RATE --- ;
 * -------------------------------------------------- ;
   if %MDX($ACSAP2D.) then do;
      TAPQ02 = 0;
      if %MDX($ACSAPPD.) then TAPQ02 = 1;
   end;
 * ----------------------------------------------------------------- ;
 * --- PQI 03 : DIABETES LONG-TERM COMPLICATIONS ADMISSION RATE  --- ;
 * ----------------------------------------------------------------- ;
   if %MDX1($ACDIALD.) then
        TAPQ03 = 1;
 * ------------------------------------------------------------------------------------------------------ ;
 * --- PQI 05 : CHRONIC OBSTRUCTIVE PULMONARY DISEASE (COPD) OR ASTHMA IN OLDER ADULTS ADMISSION RATE --- ;
 * ------------------------------------------------------------------------------------------------------ ;
   if AGE ge 40 then do;
     if %MDX1($ACCOPDD.) or %MDX1($ACSASTD.) 
        then do;  TAPQ05 = 1;
   *** Exclude Cystic Fibrosis and Anomalies 
          of the Respiratory System;
      if %MDX($RESPAN.) then TAPQ05 = .;
     end;
   end;
 * ------------------------------------------------ ;
 * --- PQI 07 : HYPERTENSION ADMISSION RATE     --- ;
 * ------------------------------------------------ ;
   if %MDX1($ACSHYPD.) then do;
        TAPQ07 = 1;
      ***Exclude Stage I-IV Kidney Disease
         with dialysis access procedures;
      if %MDX($ACSHY2D.) and %MPR($DIALY2P.)
      then TAPQ07 = .;
      *** Exclude Cardiac Procedures;
      if %MPR($ACSCARP.) then TAPQ07 = .;
   end;
 * -------------------------------------------------- ;
 * --- PQI 08 : HEART FAILURE ADMISSION RATE      --- ;
 * -------------------------------------------------- ;
   if %MDX1($MRTCHFD.) then do;
      TAPQ08 = 1;
      *** Exclude Cardiac Procedures;
      if %MPR($ACSCARP.) then TAPQ08 = .;
   end;
 * -------------------------------------------------- ;
 * --- PQI 10 : DEHYDRATION ADMISSION RATE        --- ;
 * -------------------------------------------------- ;
   if %MDX1($ACSDEHD.) or  
      (%MDX2($ACSDEHD.) and (%MDX1($HYPERID.) or %MDX1($ACPGASD.) or %MDX1($PHYSIDB.))) 
       then do;       TAPQ10 = 1;
         *** Exclude chronic renal failure ****;
         if %MDX($CRENLFD.) then TAPQ10 = .;
   end;
 * ------------------------------------------------------------ ;
 * --- PQI 11 : COMMUNITY-ACQUIRED PNEUMONIA ADMISSION RATE --- ;
 * ------------------------------------------------------------ ;
   if %MDX1($ACSBACD.) then do;
      TAPQ11 = 1;
      *** Exclude: Sickle Cell;
      if %MDX($ACSBA2D.)  then TAPQ11 = .;
      *** Exclude Immunocompromised state;
      if %MDX($IMMUNID.) or %MPR($IMMUNIP.) 
      then TAPQ11 = .;
   end;
 * ------------------------------------------------------- ;
 * --- PQI 12 : URINARY TRACK INFECTION ADMISSION RATE --- ;
 * ------------------------------------------------------- ;
   if  %MDX1($ACSUTID.) then do;
      TAPQ12 = 1;
      *** Exclude Immunocompromised state and 
          Kidney/Urinary Tract Disorder;
      if %MDX($IMMUNID.) or %MPR($IMMUNIP.) or 
         %MDX($KIDNEY.) 
      then TAPQ12 = .;
   end;
 * ----------------------------------------------------- ;
 * --- PQI 14 : UNCONTROLLED DIABETES ADMISSION RATE --- ;
 * ----------------------------------------------------- ;
   if %MDX1($ACDIAUD.) then
      TAPQ14 = 1;
 * ----------------------------------------------------------------- ;
 * --- PQI 15 : ASTHMA IN YOUNGER ADULTS DIABETES ADMISSION RATE --- ;
 * ----------------------------------------------------------------- ;
   if %MDX1($ACSASTD.) then do;
      TAPQ15 = 1;
      *** Exclude Cystic Fibrosis and Anomalies 
          of the Respiratory System;
      if %MDX($RESPAN.) then TAPQ15 = .;
      if AGE ge 40 then TAPQ15 = .;
  end;
 * ----------------------------------------------------------------------------- ;
 * --- PQI 16 : LOWER-EXTREMITY AMPUTATION AMONG PATIENTS WITH DIABETES RATE --- ;
 * ----------------------------------------------------------------------------- ;
   if %MPR($ACSLEAP.) and %MDX($ACSLEAD.) then do;
      TAPQ16 = 1;
      *** Exclude: Trauma;
      if %MDX($ACLEA2D.) then TAPQ16 = .;
   end;
 * -------------------------------------------------------------- ;
 * --- CONSTRUCT AREA LEVEL COMPOSITE INDICATORS ---------------- ;
 * -------------------------------------------------------------- ;
 * ----------------------------------------------------- ;
 * --- PQI 90 : PREVENTION QUALITY OVERALL COMPOSITE --- ;
 * ----------------------------------------------------- ;
   if TAPQ01 = 1 or TAPQ03 = 1 or TAPQ05 = 1 or TAPQ07 = 1 or 
      TAPQ08 = 1 or TAPQ10 = 1 or TAPQ11 = 1 or TAPQ12 = 1 or 
      TAPQ14 = 1 or TAPQ15 = 1 or TAPQ16 = 1
   then 
   TAPQ90 = MAX(OF TAPQ01 TAPQ03 TAPQ05 TAPQ07 
                   TAPQ08 TAPQ10 TAPQ11 TAPQ12
                   TAPQ14 TAPQ15 TAPQ16);
 * --------------------------------------------------- ;
 * --- PQI 91 : PREVENTION QUALITY ACUTE COMPOSITE --- ;
 * --------------------------------------------------- ;
   if TAPQ10 = 1 or TAPQ11 = 1 or TAPQ12 = 1 
   then 
   TAPQ91 = MAX(OF TAPQ10 TAPQ11 TAPQ12);
 * ----------------------------------------------------- ;
 * --- PQI 92 : PREVENTION QUALITY CHRONIC COMPOSITE --- ;
 * ----------------------------------------------------- ;
   if TAPQ01 = 1 or TAPQ03 = 1 or TAPQ05 = 1 or TAPQ07 = 1 or 
      TAPQ08 = 1 or TAPQ14 = 1 or TAPQ15 = 1 or 
      TAPQ16 = 1
   then 
   TAPQ92 = MAX(OF TAPQ01 TAPQ03 TAPQ05 TAPQ07 
                   TAPQ08 TAPQ14 TAPQ15
                   TAPQ16);
 * ------------------------------------------------------ ;
 * --- PQI 93 : PREVENTION QUALITY DIABETES COMPOSITE --- ;
 * ------------------------------------------------------ ;
   IF TAPQ01 = 1 OR TAPQ03 = 1 OR TAPQ14 = 1 OR TAPQ16 = 1
   THEN 
   TAPQ93 = MAX(OF TAPQ01 TAPQ03 TAPQ14 TAPQ16);
RUN;

PROC SORT DATA=&OUTFILE1.;
BY BASEID;
RUN;

PROC SUMMARY DATA=edPQI_&YEAR.;
BY BASEID;
VAR TAPQ90;
OUTPUT OUT=edACSC_&YEAR. (KEEP= BASEID TAPQ90)SUM=;
RUN;

DATA edACSC_&YEAR. (KEEP= BASEID ACSC_ED_VS);
SET edACSC_&YEAR.;
IF TAPQ90 = . THEN TAPQ90 = 0;
ACSC_ED_VS = TAPQ90;
RUN;

/* NYU Algorithm */
/***	MACROS (alter the text on the right hand side of the equals sign ONLY)		***/

/* 1 - Set to the full directory path (including all backslashes), without quotation marks, where data files are and output will be written */
%let Directory=R:\Active\mcbs\SAS\data\temp\; 

/* 2 - Set to the name of the SAS version 9 dataset (WITHOUT the libname) that contains your ED records */
%let EDdata=ed_&YEAR.;

/* 3 - Set the name of the SAS version 9 output dataset (WITHOUT the libname) that will contain your ED records and ED Algorithm Weights */
%let EDout=ed_nyu_&YEAR.;

/* 4 - Set to the name of the field that contains your Primary Discharge ICD9CM Diagnosis Codes */
%let PrimDxVarName=DX1;

/*****************************		DO NOT ALTER CODE PAST HERE			***********************************************/

libname here "&directory";


Data here.patched_ed_algo;
	INFILE "&Directory.patched_ed_algo_weights_icd10.txt" 
             DLM = '09'X DSD TRUNCOVER FIRSTOBS = 2;
    INPUT 
	dx1 		:$7. 
	edcnnpa 	:15.
	edcnpa 		:15.
	epct 		:15.
	noner 		:15.  
    injury 		:15.   
    psych 		:15.    
    alcohol 	:15.   
    drug 		:15.      
;
Run;


data temp_ed_data;
set here.&EDdata;
 length dx1 $ 7;
 dx1=left(&PrimDxVarName);
run;


proc sql;
   create table temp_ed_data_algo as 
   select	a.*									,
   			b.edcnnpa							,
			b.edcnpa							,
			b.epct								,
			b.noner								,
			b.injury							,
			b.psych								,
			b.alcohol							,
			b.drug									
      from temp_ed_data  as a 
		left join here.patched_ed_algo as b
      on a.dx1 = b.dx1	
	  order by a.dx1;
quit;


/* transforming variables for final output dataset */
Data here.&EDout (drop = tot psych);
set temp_ed_data_algo;

if edcnnpa=. then edcnnpa=0;
if edcnpa=. then edcnpa=0;
if epct=. then epct=0;
if noner=. then noner=0;
if injury=. then injury=0;
if psych=. then psych=0;
nyu_psych=psych;
if alcohol=. then alcohol=0;
if drug=. then drug=0;

/* setting up unclassified=1 as the default category if primary discharge diagnosis code is missing or not assigned nyu weights */
tot=noner+epct+edcnpa+edcnnpa+injury+nyu_psych+alcohol+drug;
if tot=0 then unclassified=1; else unclassified=0; 

label 
       edcnnpa      = "Emergent, ED Care Needed, Not Preventable/Avoidable"
	   edcnpa       = "Emergent, ED Care Needed, Preventable/Avoidable"
	   epct         = "Emergent, Primary Care Treatable"
	   noner        = "Non-Emergent"
       injury       = "Injury"
       nyu_psych    = "ED Vis Mental Health Related"
       alcohol      = "Alcohol Related"
       drug         = "Drug Related (excluding alcohol)"
       unclassified = "Not in a Special Category, and Not Classified" 
;

non_emerge = noner + epct;
if non_emerge >.5 then nyu_bal_noner = 1; else nyu_bal_noner = 0;

run;

proc sort data = temp.ed_nyu_&YEAR.;
by BASEID; 
run;

proc summary data = temp.ed_nyu_&YEAR.;
by BASEID;
var ED_VS edcnnpa edcnpa epct noner injury nyu_psych alcohol drug unclassified nyu_bal_noner;
output out=ed_nyu_&YEAR. (keep = BASEID ED_VS edcnnpa edcnpa epct noner injury nyu_psych alcohol drug unclassified nyu_bal_noner)sum=;
run;
proc means data = ed_nyu_&YEAR. n sum mean; var ED_VS edcnnpa edcnpa epct noner injury nyu_psych alcohol drug unclassified; run;

Data sample_&YEAR. (keep = BASEID CL_YR PTA_MM PTB_MM FFS_MM MA_MM MA_FLAG PRVSUP PRVSUP_MED PRVSUP_RX VA_FLAG H_MEDSTA NEW_BENE ESRD DISABLED OREC_AGE_ONLY MCAID_ANY MCAID_FULL MCAID_PART MC_ENT_ST MCR_ENT_YR MCR_ENT_MM SHSV_ACO1 NXGN_ACO CPCPL_PCMH PTD_MM 
						PD_FLAG PARTD_LIS HASPTD HASPTB HASHMO MA_INS_NM PTC_STR_RT MA_SNP MA_SNP_CD MA_SNP_DE MA_SNP_FIDE MA_SNP_IN H_DOB DOB AGE_0101 DIED DOD_YRMNDY AGE64 AGE74 AGE84 AGE85 USRD MBRZP MBRCTY MBRST SSA_SCTY RUCA1 ruca_metro1 ruca_micro1
						ruca_rural1 ADI_SCR NEWENGLAND MIDWEST WESTNORTHCENTRAL SOUTHATLANTIC EASTSCENTRAL WESTSCENTRAL MOUNTAIN PACIFIC MALE FEMALE WHITE BLACK HISPANIC ASIAN NTVAM HAWPAC MIXED OTHER 
						OTHR_LANG ENG_SPK_PRB EDUNKN EDNOHS EDHSGD EDSMCL EDCLGD INCOME FPL_LTE100 FPL_101_200 FPL_GT200 MARRIED WIDOW DIV_SEP NEV_MARRY VET_STS WORKING_SR MCBS_INST MDS3_INST NE MW SO WE HSPC 
						SNF_ST SNF_DY PTA_REIMB PTB_REIMB TOT_REIMB PTD_REIMB SHSV_ACO SV_VARSTRATUM SV_VARPSU SV_XSEE_WGT CS_VARSTRATUM CS_VARPSU CS_XSEE_WGT ALZDMT_SR DPRSSN_SR
						DM_RP DM_T1 DM_T2 DM_PRE HF_RP IH_RP ANGINA_RP CD_RP HYST_RP CLN_CNCR_RP IDD_RP PSYCH_RP ADL IADL COGIMP_SR DF_LIFT_RP DF_KNEL_RP DF_WALK_RP DF_DRES_RP DF_EAT_RP OVRWT OBESE HT_CM WT_KG BAD_SR_HLTH WRS_SR_HLTH 
						TBC_NOW PREV_SMKR ALC_PRB BLDCHL_RP BLDPRS_RP FLU_RP PNEUM_RP MAMM_RP PAPS_RP PSA_RP COL_FOBT_RP COL_FOBT_RP_LT5 COL_SCOP_RP COL_SCOP_RP_LT5 dm_ftexam dm_bldtst_self dm_slfmgt_ed dm_drvs dm_hba1c dm_cntrld PHQ8 PHQ8_NR PHQ2 PHQ2_NR GAD2 GAD2_NR PHQ_DP_DIF PRB_SLEEP MED_BILS_COLLECT HC_CST_DELAY 
						TRB_GT_CARE GPS_SCR NOMD_CST MD_CARE_HLTH MD_TRVL_EASE SPC_ACCS_SAT QUAL_SAT INFO_SAT AFRD_RPLC_FOOD AFRD_EAT_HLTH NOAFRD_EAT_LESS NOAFRD_HUNGRY OWN_HOME SECT_EGHT HOME_VALU 
						LIVE_ALON N_HHLD CS_USC_XSEE_WGT SV_USC_XSEE_WGT TUP_SCR USOC USOC_PCP USOC_SPC SPC_SR PCP_SPC_COORD USOC_VS TEST_SR TEST_FLWP_SR NEED_HOME_SR HELP_HOME_SR RX_SR INFO_RX_SR HEAR_PRB HEAR_AID CS_MED_XSEE_WGT SV_MED_XSEE_WGT SR_ED_VS_IND SR_OP_VS_IND SR_MD_VS_IND SR_MD_PCP_VS_IND SR_MD_SPC_VS_IND LTC_days LTC_SNF_days HH_VS CU_HH_CLM CU_IP_ST CU_IP_DY CU_SNF_ST CU_SNF_DY CU_ED_VS CU_CLN_VS MCAID_REIMB RX_ADRD_COV_DYS RX_ADRD_PDC 
						RX_ANDP_COV_DYS RX_ANDP_PDC AD_DX_CCW AD_DX_PRYR AMI_DX CD_DX AFB_DX BRC_DX CRC_DX ENC_DX LUC_DX PRC_DX HF_DX CKD_DX DP_DX_CCW DP_DX_CCW_DT SPDP_DX_CCW DM_DX DSL_DX HTN_DX IH_DX ART_DX SRK_DX ANX_DX_CCW ANX_DX_CCW_DT SP_DX_CCW HIV_DX ID_DX_CCW PVD_DX ALC_DX DRG_DX AD_DX AD_DX_CNT DP_DX SPDP_DX ANX_DX ID_DX ID_DX_CNT AU_DX AU_DX_CNT CP_DX CP_DX_CNT AD DM HF IH CD DP	LIV_DX CAN_DX CCI_Score HCC_Score HCC_subst HCC_psych IP_ST IP_DY CV_ST 
						CV_DY DM_ACSHP CV_ACSHP ACSHP ACSHP_ACUT ACSHP_CHRN ED_VS NYU_BAL_NONER ED_VS_PSYCH ED_VS_SUBST ACSC_ED_VS);
merge sample_&YEAR. ed_nyu_&YEAR. edACSC_&YEAR.;
by baseid;
if CL_YR = &YEAR.;
if ED_VS =. then ED_VS =0;
if nyu_bal_noner=. then nyu_bal_noner=0;
if ACSC_ED_VS=. then ACSC_ED_VS=0;
ED_VS_PSYCH = nyu_psych; if ED_VS_PSYCH=. then ED_VS_PSYCH=0;
if alchohol=. then alcohol=0; if drug=. then drug=0;
ED_VS_SUBST = alcohol + drug; if ED_VS_SUBST=. then ED_VS_SUBST=0;

run;

proc means data = sample_&YEAR. n sum mean;
var ED_VS nyu_bal_noner ACSHP ACSC_ED_VS;
run;

/* XX- Quality of Care Process Measures from carrier and outpatient claims */

/* Getting needed variables from PHY for E&M visits */
proc sql;
   create table phy_&YEAR. as 
   select	b.BASEID		,
			b.CLAIMID		,
			b.LINE_NUM		,
			b.CLM_THRU_DT	,
			b.PRVDR_SPCLTY		,
			b.BETOS_CD			,
			b.HCPCS_CD		,
			b.LINE_PLACE_OF_SRVC_CD		,
			b.PRF_PHYSN_NPI		,
			b.ICD_DGNS_CD1		,
			b.ICD_DGNS_CD2		,
			b.ICD_DGNS_CD3		,
			b.ICD_DGNS_CD4		,
			b.ICD_DGNS_CD5		,
			b.ICD_DGNS_CD6		,
			b.ICD_DGNS_CD7		,
			b.ICD_DGNS_CD8		,
			b.ICD_DGNS_CD9		,
			b.ICD_DGNS_CD10		,
			b.ICD_DGNS_CD11		,
			b.ICD_DGNS_CD12					
      from claims&YR..phy as b 
		where b.LINE_PRCSG_IND_CD in ('A','S','R') /* keeping only claim line items that were accepted for primary or secondary or final reprocessed payment */	
	order by b.BASEID, b.CLAIMID, b.LINE_NUM, b.CLM_THRU_DT;
quit;

proc sql;
   create table otp_&YEAR. as 
   select	b.BASEID		,
			b.CLAIMID		,
			b.CLM_THRU_DT		,
			b.PRVDR_NUM		, /* this is the institutional ID for the FQHC/RHC/CAH - this is what I will have to use - and assume they are primary care */
			b.CLM_FAC_TYPE_CD		,
			b.CLM_SRVC_CLSFCTN_TYPE_CD 		,
			b.PRVDR_STATE_CD		,
			b.AT_PHYSN_NPI			,
			b.ICD_DGNS_CD1		, 
			b.ICD_DGNS_CD2		,
			b.ICD_DGNS_CD3		,
			b.ICD_DGNS_CD4		,
			b.ICD_DGNS_CD5		,
			b.ICD_DGNS_CD6		,
			b.ICD_DGNS_CD7		,
			b.ICD_DGNS_CD8		,
			b.ICD_DGNS_CD9		,
			b.ICD_DGNS_CD10		,
			b.ICD_DGNS_CD11		,
			b.ICD_DGNS_CD12		,
			b.ICD_DGNS_CD13		,
			b.ICD_DGNS_CD14		,
			b.ICD_DGNS_CD15		,
			b.ICD_DGNS_CD16		,
			b.ICD_DGNS_CD17		,
			b.ICD_DGNS_CD18		,
			b.ICD_DGNS_CD19		,
			b.ICD_DGNS_CD20		,
			b.ICD_DGNS_CD21		,
			b.ICD_DGNS_CD22		,
			b.ICD_DGNS_CD23		,
			b.ICD_DGNS_CD24		,
			b.ICD_DGNS_CD25		
      from claims&YR..otp as b
      where b.CLAIMID ne .
	  		and b.CLM_MDCR_NON_PMT_RSN_CD='' /* this means the claim was not denied */
	  order by b.BASEID, b.CLAIMID, b.CLM_THRU_DT;
quit;

proc sort data = otp_&YEAR.; 
by BASEID CLAIMID; 
run;

proc sort data = claims&YR..otprev; 
by BASEID CLAIMID; 
run;

data otp_&YEAR. (keep = BASEID--CLM_THRU_DT HCPCS_CD REV_CNTR PRVDR_NUM PRF_PHYSN_NPI ICD_DGNS_CD1--ICD_DGNS_CD25);
merge otp_&YEAR. claims&YR..otprev;
by BASEID CLAIMID;
if AT_PHYSN_NPI ne ''; /*member is in the otp file above */
PRF_PHYSN_NPI = AT_PHYSN_NPI;
run;

data quality_&YEAR. (drop = LINE_NUM PRF_PHYSN_NPI PRVDR_SPCLTY CLM_THRU_DT);
set phy_&YEAR. otp_&YEAR.;

LINEITEM=LINE_NUM;
PRF_NPI=PRF_PHYSN_NPI; 
HCFASPCL=PRVDR_SPCLTY;
format FROM yymmdd10.;
FROM = CLM_THRU_DT;  

if HCPCS_CD in ('G8942','G8539','G8542','G8942','G9227','G8543') then FX_CLM=1; else FX_CLM=0; /* functional outcome assessment */
if HCPCS_CD in ('G8942','G8539','G8542','G8942') then FX_CP_CLM=1; else FX_CP_CLM=0; /* fx out assess w/care plan */
If HCPCS_CD in ('1100F','1101F','3288F','G8516','G8517') then FLLSCRN_CLM=1; else FLLSCRN_CLM=0; /* fall screen */
if HCPCS_CD in ('99497','99498','1123F','1124F') then ACP_CLM=1; else ACP_CLM=0; /* advanced care planning */
if HCPCS_CD='G8427' then RXDOC_CLM=1; else RXDOC_CLM=0;
if HCPCS_CD in ('G8733','G8734','G8941','G8735') then ELDABSC_CLM=1; else ELDABSC_CLM=0; /* elder abuse/maltreatment screen */
if HCPCS_CD in ('96160','96161') then HRA_CLM=1; else HRA_CLM=0;
if HCPCS_CD in ('99439','99487','99489','99490','99491','G2058','G0506','G2064','G2065','99495','99496','99484','99492','99493','99494','GCOL1','99483','G2214') 
then CRMG_CLM=1; 
else if REV_CNTR ne '' and HCPCS_CD='G0511' then CRMG_CLM=1;
else CRMG_CLM=0;

DP_ANX_SCRN=0;
if HCPCS_CD in ('G0444','96127') then DP_ANX_SCRN=1;
array diag{25} ICD_DGNS_CD1-ICD_DGNS_CD25;
do i=1 to 25;
if diag{i} in ('Z133','Z1330','Z1331','Z1332','Z1339') then DP_ANX_SCRN=1; end;

PSYCHO_THR_VS=0;
if HCPCS_CD in ('90785','90791','90792','90832','90833','90834','90836','90837','90838','90839 ','90840','90845','90846','90847','90849','90853','90885','90887','90889','90899')
then PSYCHO_THR_VS=1;

CCM_BHI_CLM=0;
if HCPCS_CD in ('99492','99493','99494','G0512','99484','G0511') then CCM_BHI_CLM=1;

run;

/* dealing with duplicate records on the same day without losing the information I need*/
proc sort data = quality_&YEAR.;
by BASEID CLAIMID FROM;
run;

proc summary data = quality_&YEAR.;
by BASEID CLAIMID FROM;
var FX_CLM FX_CP_CLM FLLSCRN_CLM ACP_CLM RXDOC_CLM ELDABSC_CLM HRA_CLM CRMG_CLM DP_ANX_SCRN PSYCHO_THR_VS CCM_BHI_CLM;
output out=quality_&YEAR. (keep= BASEID CLAIMID FROM FX_CLM FX_CP_CLM FLLSCRN_CLM ACP_CLM RXDOC_CLM ELDABSC_CLM HRA_CLM CRMG_CLM DP_ANX_SCRN PSYCHO_THR_VS CCM_BHI_CLM)sum=;
run;

data quality_&YEAR.;
set quality_&YEAR.;
if FX_CLM >1 then FX_CLM =1;
if FX_CP_CLM >1 then FX_CP_CLM =1; 
if FLLSCRN_CLM >1 then FLLSCRN_CLM =1; 
if ACP_CLM >1 then ACP_CLM =1; 
if RXDOC_CLM >1 then RXDOC_CLM =1; 
if ELDABSC_CLM >1 then ELDABSC_CLM =1; 
if HRA_CLM >1 then HRA_CLM =1; 
if CRMG_CLM >1 then CRMG_CLM =1;
if DP_ANX_SCRN>1 then DP_ANX_SCRN=1;
if PSYCHO_THR_VS>1 then PSYCHO_THR_VS=1;
if CCM_BHI_CLM>1 then CCM_BHI_CLM=1;
run;

proc sort data = quality_&YEAR.;
by BASEID;
run;

proc summary data = quality_&YEAR.;
by BASEID;
var FX_CLM FX_CP_CLM FLLSCRN_CLM ACP_CLM RXDOC_CLM ELDABSC_CLM HRA_CLM CRMG_CLM DP_ANX_SCRN PSYCHO_THR_VS CCM_BHI_CLM;
output out=quality_&YEAR. (keep= BASEID FX_CLM FX_CP_CLM FLLSCRN_CLM ACP_CLM RXDOC_CLM ELDABSC_CLM HRA_CLM CRMG_CLM DP_ANX_SCRN PSYCHO_THR_VS CCM_BHI_CLM)sum=;
run;

Data sample_&YEAR. (keep = BASEID CL_YR PTA_MM PTB_MM FFS_MM MA_MM MA_FLAG PRVSUP PRVSUP_MED PRVSUP_RX VA_FLAG H_MEDSTA NEW_BENE ESRD DISABLED OREC_AGE_ONLY MCAID_ANY MCAID_FULL MCAID_PART MC_ENT_ST MCR_ENT_YR MCR_ENT_MM SHSV_ACO1 NXGN_ACO CPCPL_PCMH PTD_MM 
						PD_FLAG PARTD_LIS HASPTD HASPTB HASHMO MA_INS_NM PTC_STR_RT MA_SNP MA_SNP_CD MA_SNP_DE MA_SNP_FIDE MA_SNP_IN H_DOB DOB AGE_0101 DIED DOD_YRMNDY AGE64 AGE74 AGE84 AGE85 USRD MBRZP MBRCTY MBRST SSA_SCTY RUCA1 ruca_metro1 ruca_micro1
						ruca_rural1 ADI_SCR NEWENGLAND MIDWEST WESTNORTHCENTRAL SOUTHATLANTIC EASTSCENTRAL WESTSCENTRAL MOUNTAIN PACIFIC MALE FEMALE WHITE BLACK HISPANIC ASIAN NTVAM HAWPAC MIXED OTHER 
						OTHR_LANG ENG_SPK_PRB EDUNKN EDNOHS EDHSGD EDSMCL EDCLGD INCOME FPL_LTE100 FPL_101_200 FPL_GT200 MARRIED WIDOW DIV_SEP NEV_MARRY VET_STS WORKING_SR MCBS_INST MDS3_INST NE MW SO WE HSPC 
						SNF_ST SNF_DY PTA_REIMB PTB_REIMB TOT_REIMB PTD_REIMB SHSV_ACO SV_VARSTRATUM SV_VARPSU SV_XSEE_WGT CS_VARSTRATUM CS_VARPSU CS_XSEE_WGT ALZDMT_SR DPRSSN_SR
						DM_RP DM_T1 DM_T2 DM_PRE HF_RP IH_RP ANGINA_RP CD_RP HYST_RP CLN_CNCR_RP IDD_RP PSYCH_RP ADL IADL COGIMP_SR DF_LIFT_RP DF_KNEL_RP DF_WALK_RP DF_DRES_RP DF_EAT_RP OVRWT OBESE HT_CM WT_KG BAD_SR_HLTH WRS_SR_HLTH 
						TBC_NOW PREV_SMKR ALC_PRB BLDCHL_RP BLDPRS_RP FLU_RP PNEUM_RP MAMM_RP PAPS_RP PSA_RP COL_FOBT_RP COL_FOBT_RP_LT5 COL_SCOP_RP COL_SCOP_RP_LT5 dm_ftexam dm_bldtst_self dm_slfmgt_ed dm_drvs dm_hba1c dm_cntrld PHQ8 PHQ8_NR PHQ2 PHQ2_NR GAD2 GAD2_NR PHQ_DP_DIF PRB_SLEEP MED_BILS_COLLECT HC_CST_DELAY 
						TRB_GT_CARE GPS_SCR NOMD_CST MD_CARE_HLTH MD_TRVL_EASE SPC_ACCS_SAT QUAL_SAT INFO_SAT AFRD_RPLC_FOOD AFRD_EAT_HLTH NOAFRD_EAT_LESS NOAFRD_HUNGRY OWN_HOME SECT_EGHT HOME_VALU 
						LIVE_ALON N_HHLD CS_USC_XSEE_WGT SV_USC_XSEE_WGT TUP_SCR USOC USOC_PCP USOC_SPC SPC_SR PCP_SPC_COORD USOC_VS TEST_SR TEST_FLWP_SR NEED_HOME_SR HELP_HOME_SR RX_SR INFO_RX_SR HEAR_PRB HEAR_AID CS_MED_XSEE_WGT SV_MED_XSEE_WGT SR_ED_VS_IND SR_OP_VS_IND SR_MD_VS_IND SR_MD_PCP_VS_IND SR_MD_SPC_VS_IND LTC_days LTC_SNF_days HH_VS CU_HH_CLM CU_IP_ST CU_IP_DY CU_SNF_ST CU_SNF_DY CU_ED_VS CU_CLN_VS MCAID_REIMB RX_ADRD_COV_DYS RX_ADRD_PDC 
						RX_ANDP_COV_DYS RX_ANDP_PDC AD_DX_CCW AD_DX_PRYR AMI_DX CD_DX AFB_DX BRC_DX CRC_DX ENC_DX LUC_DX PRC_DX HF_DX CKD_DX DP_DX_CCW DP_DX_CCW_DT SPDP_DX_CCW DM_DX DSL_DX HTN_DX IH_DX ART_DX SRK_DX ANX_DX_CCW ANX_DX_CCW_DT SP_DX_CCW HIV_DX ID_DX_CCW PVD_DX ALC_DX DRG_DX AD_DX AD_DX_CNT DP_DX SPDP_DX ANX_DX ID_DX ID_DX_CNT AU_DX AU_DX_CNT CP_DX CP_DX_CNT AD DM HF IH CD DP	LIV_DX CAN_DX CCI_Score HCC_Score HCC_subst HCC_psych IP_ST IP_DY CV_ST 
						CV_DY DM_ACSHP CV_ACSHP ACSHP ACSHP_ACUT ACSHP_CHRN ED_VS NYU_BAL_NONER ED_VS_PSYCH ED_VS_SUBST ACSC_ED_VS FX_CLM FX_CP_CLM FLLSCRN_CLM ACP_CLM RXDOC_CLM ELDABSC_CLM HRA_CLM CRMG_CLM DP_ANX_SCRN PSYCHO_THR_VS CCM_BHI_CLM);
merge sample_&YEAR. quality_&YEAR.;
by baseid;
if CL_YR = &YEAR.;
if FX_CLM=. then FX_CLM =0;
if FX_CP_CLM=. then FX_CP_CLM =0; 
if FLLSCRN_CLM=. then FLLSCRN_CLM =0; 
if ACP_CLM=. then ACP_CLM =0; 
if RXDOC_CLM=. then RXDOC_CLM =0; 
if ELDABSC_CLM=. then ELDABSC_CLM =0; 
if HRA_CLM=. then HRA_CLM =0; 
if CRMG_CLM=. then CRMG_CLM =0;
if DP_ANX_SCRN=. then DP_ANX_SCRN=0;
if PSYCHO_THR_VS=. then PSYCHO_THR_VS=0;
if CCM_BHI_CLM=. then CCM_BHI_CLM=0;

run;

proc means data = sample_&YEAR. n sum mean;
var FX_CLM FX_CP_CLM FLLSCRN_CLM ACP_CLM RXDOC_CLM ELDABSC_CLM HRA_CLM CRMG_CLM DP_ANX_SCRN PSYCHO_THR_VS CCM_BHI_CLM;
run;

/* 20a - identifying total # of E&M visits in physician claims - and physician specialty types */
/* alternate identification of physician specialty types - based on reported primary and secondary specialties on physician compare and NPPES */
proc sort data = temp.phy_ger_psychia_neuro; by PRF_NPI; run;
proc sort data = temp.phy_em_&YEAR.; by PRF_NPI; run;

data eandm_&YEAR. (keep = BASEID FROM PRF_NPI HCFASPCL EM_TTL EM_PCP EM_APN EM_DM EM_HF EM_IH EM_CD EM_GER EM_PSYCHIA EM_NEURO EM_SCP EM_PRE_SEPT EM_FQRHC AWV_TTL EM_HOME_VS EM_VIRT_VS EM_COGIMP_CP ACO_EM_ELG 
				   DEP_EM_VS ANX_EM_VS DEP_ANX_EM_VS DP_PSYCH DP_PCPMD DP_APN DP_OTHSPC ANX_PSYCH ANX_PCPMD ANX_APN ANX_OTHSPC DEP_ANX_PSYCH DEP_ANX_PCPMD DEP_ANX_APN DEP_ANX_OTHSPC AD_EM_VS HCFASPCL2 SCP_CRDILGY SCP_PUL_DIS SCP_ENDCRIN SCP_NEPHRLG SCP_OPTHMLG SCP_PODIATR
  				   SCP_INF_DIS SCP_UROLOGY SCP_DRMTLGY SCP_NEUROLG SCP_OTO_LRY SCP_GASTROE SCP_ALG_IMM SCP_RHEUMTG SCP_PRPHVSC SCP_ONCOL SCP_ORTHO SCP_OPTOM SCP_MLT_GRP SCP_NRS_SPC 
				   SCP_PSYCHIA SCP_PSYCHOL SCP_OTHER);
merge temp.phy_em_&YEAR. temp.phy_ger_psychia_neuro;
by PRF_NPI;
if ClaimID ne .; /* record exists in the physician E&M dataset */

EM_TTL=1;
rev=substr(REV_CNTR,1,3);
if HCFASPCL in ('01','08','11','37','84','16','42') or REV_CNTR in ('0521','0522','0525') or rev in ('096','097','098') then EM_PCP=1; else EM_PCP=0;
/* for PCP limit HFCA spec type to GP, FP, Int Med, Peds, Prev Med, ObGyn, CN Midwif, FQHC, RHC, CAH*/
if HCFASPCL in ('50','97') or apn_pcp=1 then EM_APN=1; else EM_APN=0; /* nurse practitioner and physician assistant */ if EM_APN=1 then EM_PCP=0; /* can't be both a physician and non-physician provider */
if HCFASPCL in ('39','46') then EM_DM=1; else EM_DM=0;/*  limit the HFCA specialty type to  Endo, Nephr */
if HCFASPCL ='06' then EM_HF=1; else EM_HF=0; /*  limit the HFCA specialty type to cardiologist */
EM_IH=EM_HF; /*  limit the HFCA specialty type to cardiologist */
if HCFASPCL ='29' then EM_CD=1; else EM_CD=0; /*  limit the HFCA specialty type to pulmonary disease */
if HCFASPCL='38' or phy_geriatrics=1 then EM_GER=1; else EM_GER=0; if EM_GER=1 then EM_PCP=0; /* if the HFCA specialty type or NPI NPPES taxonomy code is for a geriatrician then it is a geriatrics E&M visit */
if HCFASPCL in ('26','27','86') or phy_psychiatrist=1 then EM_PSYCHIA=1; else EM_PSYCHIA=0; /* if the HFCA specialty type or NPI NPPES taxonomy code is for a psyciatrist then it is a psychiatry E&M visit */
if HCFASPCL in ('13','14','86') or phy_neuro=1 then EM_NEURO=1; else EM_NEURO=0; /* if the HFCA specialty type or NPI NPPES taxonomy code is for a neurologist then it is a neurology E&M visit */
if EM_PCP=0 and EM_GER=0 and EM_APN=0 then EM_SCP = 1; /* counting # of specialist E&M visits  */
if FROM < MDY(09,01,&YEAR.) then EM_PRE_SEPT=1; else EM_PRE_SEPT=0;
if REV_CNTR in ('0521','0522','0525') or rev in ('096','097','098') then EM_FQRHC=1; else EM_FQRHC=0;

AWV_TTL=0;
if HCPCS_CD in ('G0402','G0438','G0439') then AWV_TTL=1; /* Medicare annual wellness visit HCPCS codes used by all */
if REV_CNTR ne '' and HCPCS_CD='G0468' then AWV_TTL=1; /* Medicare annual wellness visit HCPCS codes may be used by FQHCs/RHCs */

EM_HOME_VS=0;
if HCPCS_CD in ('99324','99325','99326','99327','99328','99334','99335','99336','99337','99339','99340','99341','99342','99343','99344','99345','99347','99348','99349','99350') then EM_HOME_VS=1;
else if REV_CNTR='0522' then EM_HOME_VS=1;

EM_VIRT_VS=0;
if HCPCS_CD in ('99421','99422','99423','99441','99442','99443','G2010','G2012') then EM_VIRT_VS=1;

EM_COGIMP_CP=0;
if HCPCS_CD='99483' then EM_COGIMP_CP=1;

ACO_EM_ELG=0;
if HCFASPCL in ('01','08','11','37','38','50','89','97') or REV_CNTR in ('0521','0522','0525') or rev in ('096','097','098') then ACO_EM_ELG=1;
else if HCFASPCL in ('06','12','13','16','23','25','26','27','29','39','46','70','79','82','83','84','86','90','98') then ACO_EM_ELG=1;

/* Depression-related E&M visits with Psychiatrist, PCP physician, PCP NP/PA, Clinical nurse specialist, geriatirician, neurologist, other specialist physician */
DEP_EM_VS=0;
array diag{12} ICD_DGNS_CD1-ICD_DGNS_CD12;
do i=1 to 12;
	if diag{i} in ('F0631','F0632','F3010','F3011','F3012','F3013','F302','F303','F304','F308','F309','F310','F3110','F3111','F3112','F3113','F312',
'F3130','F3131','F3132','F314','F315','F3160','F3161','F3162','F3163','F3164','F3170','F3171','F3172','F3173','F3174','F3175',
'F3176','F3177','F3178','F3181','F3189','F319','F320','F321','F322','F323','F324','F325','F328','F3289','F329','F32A','F330',
'F331','F332','F333','F3340','F3341','F3342','F338','F339','F340','F341','F3481','F3489','F349','F39','F4321','F4323')
	then DEP_EM_VS=1;  /* E&M visits with a diagnosis code for depression somewhere on the claim */
		if diag{i} in ('F064','F4000','F4001','F4002',
				   'F4010','F4011','F40210','F40218','F40220','F40228','F40230','F40231','F40232','F40233','F40240','F40241','F40242','F40243',
				   'F40248','F40290','F40291','F40298','F408','F409','F410','F411','F413','F418','F419','F42','F422','F423','F424','F428','F429',
				   'F430','F4310','F4311','F4312','F449','F458','F488','F489','F938','F99','R452','R455','R456','R457')
	then ANX_EM_VS=1;  /* E&M visits with a diagnosis code for anxiety somewhere on the claim */
end;
if DEP_EM_VS=1 or ANX_EM_VS=1 then DEP_ANX_EM_VS=1; else DEP_ANX_EM_VS=0;

if DEP_EM_VS=1 and (HCFASPCL in ('26','27','86') or phy_psychiatrist=1) and (HCFASPCL not in ('50','97','89')) then DP_PSYCH=1; else DP_PSYCH=0;
rev=substr(REV_CNTR,1,3);
if DEP_EM_VS=1 and (HCFASPCL in ('01','08','11','37','38','84','16','42') or REV_CNTR in ('0521','0522','0525') or rev in ('096','097','098')) and (HCFASPCL not in ('50','97','89')) 
and (DP_PSYCH ne 1) then DP_PCPMD=1; else DP_PCPMD=0;
if DEP_EM_VS=1 and HCFASPCL in ('50','89','97') then DP_APN=1; else DP_APN=0; /* nurse practitioner or physician assistant or clinical nurse specialist */
if DEP_EM_VS=1 and DP_PSYCH=0 and DP_PCPMD=0 and DP_APN=0 then DP_OTHSPC=1; else DP_OTHSPC=0;

if ANX_EM_VS=1 and (HCFASPCL in ('26','27','86') or phy_psychiatrist=1) and (HCFASPCL not in ('50','97','89')) then ANX_PSYCH=1; else ANX_PSYCH=0;
rev=substr(REV_CNTR,1,3);
if ANX_EM_VS=1 and (HCFASPCL in ('01','08','11','37','38','84','16','42') or REV_CNTR in ('0521','0522','0525') or rev in ('096','097','098')) and (HCFASPCL not in ('50','97','89')) 
and (ANX_PSYCH ne 1) then ANX_PCPMD=1; else ANX_PCPMD=0;
if ANX_EM_VS=1 and HCFASPCL in ('50','89','97') then ANX_APN=1; else ANX_APN=0; /* nurse practitioner or physician assistant or clinical nurse specialist */
if ANX_EM_VS=1 and ANX_PSYCH=0 and ANX_PCPMD=0 and ANX_APN=0 then ANX_OTHSPC=1; else ANX_OTHSPC=0;

if DEP_ANX_EM_VS=1 and (HCFASPCL in ('26','27','86') or phy_psychiatrist=1) and (HCFASPCL not in ('50','97','89')) then DEP_ANX_PSYCH=1; else DEP_ANX_PSYCH=0;
rev=substr(REV_CNTR,1,3);
if DEP_ANX_EM_VS=1 and (HCFASPCL in ('01','08','11','37','38','84','16','42') or REV_CNTR in ('0521','0522','0525') or rev in ('096','097','098')) and (HCFASPCL not in ('50','97','89')) 
and (ANX_PSYCH ne 1) then DEP_ANX_PCPMD=1; else DEP_ANX_PCPMD=0;
if DEP_ANX_EM_VS=1 and HCFASPCL in ('50','89','97') then DEP_ANX_APN=1; else DEP_ANX_APN=0; /* nurse practitioner or physician assistant or clinical nurse specialist */
if DEP_ANX_EM_VS=1 and DEP_ANX_PSYCH=0 and DEP_ANX_PCPMD=0 and DEP_ANX_APN=0 then DEP_ANX_OTHSPC=1; else DEP_ANX_OTHSPC=0;

/* Demenentia-related E&M visits */
AD_EM_VS=0;
do i=1 to 12;
	if diag{i} in ('F0150','F0151','F0280','F0281','F0390','F0391','F04','G138','F05','F061','F068','G300','G301','G308','G309','G311','G312',
					'G3101','G3109','G94','R4181','R54')
	then AD_EM_VS=1;  /* E&M visits with a diagnosis code for ADRD somewhere on the claim */
end;

HCFASPCL2=HCFASPCL;
if HCFASPCL in('23','76') then HCFASPCL2='XX'; /* setting the 2 PVD specialty types = to 1*/
if HCFASPCL in('90','91','92','98') then HCFASPCL2='YY'; /* setting the 3 oncology specialty types = to 1*/
if HCFASPCL in ('26','27','86') then HCFASPCL2='ZZ'; /* settign the three psychiatrist types = to 1 */
if HCFASPCL in ('62','68','80') then HCFASPCL2='WW'; /* settign the three psychologist types = to 1 */
if HCFASPCL='06' then SCP_CRDILGY=1; else SCP_CRDILGY=0;
if HCFASPCL='29' then SCP_PUL_DIS=1; else SCP_PUL_DIS=0;
if HCFASPCL='46' then SCP_ENDCRIN=1; else SCP_ENDCRIN=0;
if HCFASPCL='39' then SCP_NEPHRLG=1; else SCP_NEPHRLG=0;
if HCFASPCL='18' then SCP_OPTHMLG=1; else SCP_OPTHMLG=0;
if HCFASPCL='48' then SCP_PODIATR=1; else SCP_PODIATR=0;
if HCFASPCL='44' then SCP_INF_DIS=1; else SCP_INF_DIS=0;
if HCFASPCL='34' then SCP_UROLOGY=1; else SCP_UROLOGY=0;
if HCFASPCL='07' then SCP_DRMTLGY=1; else SCP_DRMTLGY=0;
if HCFASPCL='13' then SCP_NEUROLG=1; else SCP_NEUROLG=0;
if HCFASPCL='04' then SCP_OTO_LRY=1; else SCP_OTO_LRY=0;
if HCFASPCL='10' then SCP_GASTROE=1; else SCP_GASTROE=0;
if HCFASPCL='03' then SCP_ALG_IMM=1; else SCP_ALG_IMM=0;
if HCFASPCL='66' then SCP_RHEUMTG=1; else SCP_RHEUMTG=0;
if HCFASPCL in ('23','76') then SCP_PRPHVSC=1; else SCP_PRPHVSC=0;
if HCFASPCL in ('90','91','92','98') then SCP_ONCOL=1; else SCP_ONCOL=0;
if HCFASPCL='20' then SCP_ORTHO=1; else SCP_ORTHO=0;
if HCFASPCL='41' then SCP_OPTOM=1; else SCP_OPTOM=0;
if HCFASPCL='70' then SCP_MLT_GRP=1; else SCP_MLT_GRP=0;
if HCFASPCL='89' then SCP_NRS_SPC=1; else SCP_NRS_SPC=0;
if HCFASPCL in ('26','27','86') then SCP_PSYCHIA=1; else SCP_PSYCHIA=0; /* psychiatry = psych, geriatric psych, neuropsych */
if HCFASPCL in ('62','68','80') then SCP_PSYCHOL=1; else SCP_PSYCHOL=0; /* psychology = psychologist, clinical psychologist, LCSW */
if EM_PCP=0 and HCFASPCL not in ('','20','41','23','76','06','29','46','39','18','48','44','34','07','13','04','10','03','66','70','89','90','91','92','98','26','27','86','62','68','80') 
then SCP_OTHER = 1; else SCP_OTHER = 0;

run;

proc sort data = eandm_&YEAR. nodupkey; /* eliminating duplicate same day provider visits */
by BASEID PRF_NPI FROM;
run;

/* summing E&M visits */
proc sort data = eandm_&YEAR.;
by BASEID; 
run;

proc summary data = eandm_&YEAR.;
by BASEID;
var EM_TTL EM_PCP EM_APN EM_DM EM_HF EM_IH EM_CD EM_GER EM_PSYCHIA EM_NEURO EM_SCP EM_PRE_SEPT EM_FQRHC AWV_TTL EM_HOME_VS EM_VIRT_VS EM_COGIMP_CP ACO_EM_ELG DEP_EM_VS ANX_EM_VS DEP_ANX_EM_VS DP_PSYCH DP_PCPMD DP_APN DP_OTHSPC ANX_PSYCH ANX_PCPMD ANX_APN ANX_OTHSPC DEP_ANX_PSYCH DEP_ANX_PCPMD DEP_ANX_APN DEP_ANX_OTHSPC AD_EM_VS;
output out=eandm_sum_&YEAR. (keep = BASEID EM_TTL EM_PCP EM_APN EM_DM EM_HF EM_IH EM_CD EM_GER EM_PSYCHIA EM_NEURO EM_SCP EM_PRE_SEPT EM_FQRHC AWV_TTL EM_HOME_VS EM_VIRT_VS EM_COGIMP_CP ACO_EM_ELG DEP_EM_VS ANX_EM_VS DEP_ANX_EM_VS DP_PSYCH DP_PCPMD DP_APN DP_OTHSPC ANX_PSYCH ANX_PCPMD ANX_APN ANX_OTHSPC DEP_ANX_PSYCH DEP_ANX_PCPMD DEP_ANX_APN DEP_ANX_OTHSPC AD_EM_VS)sum=;
run;

/* summing up distinct clinicians visited */
data labor_&YEAR. (keep = BASEID FROM PRF_NPI DSTNCT_CLIN DSTNCT_PCP DSTNCT_APN DSTNCT_SCP);
set eandm_&YEAR.;
DSTNCT_CLIN=EM_TTL;
DSTNCT_PCP=EM_PCP+EM_GER;
DSTNCT_APN=EM_APN;
DSTNCT_SCP=EM_SCP;
run;

proc sort data = labor_&YEAR. nodupkey; /* noduping at the patient-provider level to count distinct physicians visited in a year */
by BASEID PRF_NPI;
run;

proc sort data = labor_&YEAR.;
by BASEID;
run;

proc summary data = labor_&YEAR.;
by BASEID;
var DSTNCT_CLIN DSTNCT_PCP DSTNCT_APN DSTNCT_SCP;
output out=labor_&YEAR. (keep = BASEID DSTNCT_CLIN DSTNCT_PCP DSTNCT_APN DSTNCT_SCP)sum=;
run;

/*summing specialist physician speciality types visited */
data dummy_type_&YEAR.;
set eandm_&YEAR.;
if HCFASPCL in ('20','41','23','76','06','29','46','39','18','48','44','34','07','13','04','10','03','66','89','90','91','92','98','26','27','86','62','68','80') 
then DSTNCT_SCP_TYP=1; else DSTNCT_SCP_TYP=0; /* these are the key specialist specialty types apart from multi-group specialist*/
run;

proc sort data = dummy_type_&YEAR. nodupkey; /* noduping at the patient-provider type level to count distinct physician dummies visited in a year */
by BASEID HCFASPCL2;
run;

proc sort data = dummy_type_&YEAR.;
by BASEID;
run;

proc summary data = dummy_type_&YEAR.;
by BASEID;
var DSTNCT_SCP_TYP SCP_CRDILGY SCP_PUL_DIS SCP_ENDCRIN SCP_NEPHRLG SCP_OPTHMLG SCP_PODIATR SCP_INF_DIS SCP_UROLOGY SCP_DRMTLGY SCP_NEUROLG SCP_OTO_LRY SCP_GASTROE SCP_ALG_IMM SCP_RHEUMTG 
SCP_PRPHVSC SCP_ONCOL SCP_ORTHO SCP_OPTOM SCP_MLT_GRP SCP_NRS_SPC SCP_PSYCHIA SCP_PSYCHOL SCP_OTHER;
output out=dummy_type_&YEAR. (keep = BASEID DSTNCT_SCP_TYP SCP_CRDILGY SCP_PUL_DIS SCP_ENDCRIN SCP_NEPHRLG SCP_OPTHMLG SCP_PODIATR SCP_INF_DIS SCP_UROLOGY SCP_DRMTLGY SCP_NEUROLG SCP_OTO_LRY 
SCP_GASTROE SCP_ALG_IMM SCP_RHEUMTG SCP_PRPHVSC SCP_ONCOL SCP_ORTHO SCP_OPTOM SCP_MLT_GRP SCP_NRS_SPC SCP_PSYCHIA SCP_PSYCHOL SCP_OTHER)sum=;
run;

Data sample_&YEAR. (keep = BASEID CL_YR PTA_MM PTB_MM FFS_MM MA_MM MA_FLAG PRVSUP PRVSUP_MED PRVSUP_RX VA_FLAG H_MEDSTA NEW_BENE ESRD DISABLED OREC_AGE_ONLY MCAID_ANY MCAID_FULL MCAID_PART MC_ENT_ST MCR_ENT_YR MCR_ENT_MM SHSV_ACO1 NXGN_ACO CPCPL_PCMH PTD_MM 
						PD_FLAG PARTD_LIS HASPTD HASPTB HASHMO MA_INS_NM PTC_STR_RT MA_SNP MA_SNP_CD MA_SNP_DE MA_SNP_FIDE MA_SNP_IN H_DOB DOB AGE_0101 DIED DOD_YRMNDY AGE64 AGE74 AGE84 AGE85 USRD MBRZP MBRCTY MBRST SSA_SCTY RUCA1 ruca_metro1 ruca_micro1
						ruca_rural1 ADI_SCR NEWENGLAND MIDWEST WESTNORTHCENTRAL SOUTHATLANTIC EASTSCENTRAL WESTSCENTRAL MOUNTAIN PACIFIC MALE FEMALE WHITE BLACK HISPANIC ASIAN NTVAM HAWPAC MIXED OTHER 
						OTHR_LANG ENG_SPK_PRB EDUNKN EDNOHS EDHSGD EDSMCL EDCLGD INCOME FPL_LTE100 FPL_101_200 FPL_GT200 MARRIED WIDOW DIV_SEP NEV_MARRY VET_STS WORKING_SR MCBS_INST MDS3_INST NE MW SO WE HSPC 
						SNF_ST SNF_DY PTA_REIMB PTB_REIMB TOT_REIMB PTD_REIMB SHSV_ACO SV_VARSTRATUM SV_VARPSU SV_XSEE_WGT CS_VARSTRATUM CS_VARPSU CS_XSEE_WGT ALZDMT_SR DPRSSN_SR
						DM_RP DM_T1 DM_T2 DM_PRE HF_RP IH_RP ANGINA_RP CD_RP HYST_RP CLN_CNCR_RP IDD_RP PSYCH_RP ADL IADL COGIMP_SR DF_LIFT_RP DF_KNEL_RP DF_WALK_RP DF_DRES_RP DF_EAT_RP OVRWT OBESE HT_CM WT_KG BAD_SR_HLTH WRS_SR_HLTH 
						TBC_NOW PREV_SMKR ALC_PRB BLDCHL_RP BLDPRS_RP FLU_RP PNEUM_RP MAMM_RP PAPS_RP PSA_RP COL_FOBT_RP COL_FOBT_RP_LT5 COL_SCOP_RP COL_SCOP_RP_LT5 dm_ftexam dm_bldtst_self dm_slfmgt_ed dm_drvs dm_hba1c dm_cntrld PHQ8 PHQ8_NR PHQ2 PHQ2_NR GAD2 GAD2_NR PHQ_DP_DIF PRB_SLEEP MED_BILS_COLLECT HC_CST_DELAY 
						TRB_GT_CARE GPS_SCR NOMD_CST MD_CARE_HLTH MD_TRVL_EASE SPC_ACCS_SAT QUAL_SAT INFO_SAT AFRD_RPLC_FOOD AFRD_EAT_HLTH NOAFRD_EAT_LESS NOAFRD_HUNGRY OWN_HOME SECT_EGHT HOME_VALU 
						LIVE_ALON N_HHLD CS_USC_XSEE_WGT SV_USC_XSEE_WGT TUP_SCR USOC USOC_PCP USOC_SPC SPC_SR PCP_SPC_COORD USOC_VS TEST_SR TEST_FLWP_SR NEED_HOME_SR HELP_HOME_SR RX_SR INFO_RX_SR HEAR_PRB HEAR_AID CS_MED_XSEE_WGT SV_MED_XSEE_WGT SR_ED_VS_IND SR_OP_VS_IND SR_MD_VS_IND SR_MD_PCP_VS_IND SR_MD_SPC_VS_IND LTC_days LTC_SNF_days HH_VS CU_HH_CLM CU_IP_ST CU_IP_DY CU_SNF_ST CU_SNF_DY CU_ED_VS CU_CLN_VS MCAID_REIMB RX_ADRD_COV_DYS RX_ADRD_PDC 
						RX_ANDP_COV_DYS RX_ANDP_PDC AD_DX_CCW AD_DX_PRYR AMI_DX CD_DX AFB_DX BRC_DX CRC_DX ENC_DX LUC_DX PRC_DX HF_DX CKD_DX DP_DX_CCW DP_DX_CCW_DT SPDP_DX_CCW DM_DX DSL_DX HTN_DX IH_DX ART_DX SRK_DX ANX_DX_CCW ANX_DX_CCW_DT SP_DX_CCW HIV_DX ID_DX_CCW PVD_DX ALC_DX DRG_DX AD_DX AD_DX_CNT DP_DX SPDP_DX ANX_DX ID_DX ID_DX_CNT AU_DX AU_DX_CNT CP_DX CP_DX_CNT AD DM HF IH CD DP	LIV_DX CAN_DX CCI_Score HCC_Score HCC_subst HCC_psych IP_ST IP_DY CV_ST 
						CV_DY DM_ACSHP CV_ACSHP ACSHP ACSHP_ACUT ACSHP_CHRN ED_VS NYU_BAL_NONER ED_VS_PSYCH ED_VS_SUBST ACSC_ED_VS FX_CLM FX_CP_CLM FLLSCRN_CLM ACP_CLM RXDOC_CLM ELDABSC_CLM HRA_CLM CRMG_CLM DP_ANX_SCRN PSYCHO_THR_VS CCM_BHI_CLM EM_TTL EM_PCP EM_APN EM_DM EM_HF EM_IH EM_CD EM_GER EM_PSYCHIA EM_NEURO EM_SCP EM_PRE_SEPT EM_FQRHC AWV_TTL EM_HOME_VS EM_VIRT_VS EM_COGIMP_CP ACO_EM_ELG DEP_EM_VS ANX_EM_VS DEP_ANX_EM_VS DP_PSYCH DP_PCPMD DP_APN DP_OTHSPC ANX_PSYCH ANX_PCPMD ANX_APN ANX_OTHSPC DEP_ANX_PSYCH DEP_ANX_PCPMD DEP_ANX_APN DEP_ANX_OTHSPC AD_EM_VS DSTNCT_CLIN DSTNCT_PCP DSTNCT_APN DSTNCT_SCP DSTNCT_SCP_TYP SCP_CRDILGY SCP_PUL_DIS SCP_ENDCRIN
						SCP_NEPHRLG SCP_OPTHMLG SCP_PODIATR SCP_INF_DIS SCP_UROLOGY SCP_DRMTLGY SCP_NEUROLG SCP_OTO_LRY SCP_GASTROE SCP_ALG_IMM SCP_RHEUMTG SCP_PRPHVSC SCP_ONCOL 
						SCP_ORTHO SCP_OPTOM SCP_MLT_GRP SCP_NRS_SPC SCP_PSYCHIA SCP_PSYCHOL SCP_OTHER);
merge sample_&YEAR. eandm_sum_&YEAR. labor_&YEAR. dummy_type_&YEAR.;
by baseid;
if CL_YR = &YEAR.;
if EM_TTL =. then EM_TTL =0;
if EM_PCP =. then EM_PCP =0;
if EM_APN =. then EM_APN =0;
if EM_DM =. then EM_DM =0;
if EM_HF =. then EM_HF =0;
if EM_IH =. then EM_IH =0;
if EM_CD =. then EM_CD =0;
if EM_GER =. then EM_GER =0;
if EM_PSYCHIA=. then EM_PSYCHIA=0;
if EM_NEURO=. then EM_NEURO=0;
if EM_SCP=. then EM_SCP=0;
if EM_PRE_SEPT=. then EM_PRE_SEPT=0;
if EM_FQRHC=. then EM_FQRHC=0;
if AWV_TTL=. then AWV_TTL=0;
if EM_HOME_VS=. then EM_HOME_VS=0;
if EM_CRMG_VS=. then EM_CRMG_VS=0;
if EM_VIRT_VS=. then EM_VIRT_VS=0;
if EM_COGIMP_CP=. then EM_COGIMP_CP=0; else if EM_COGIMP_CP>1 then EM_COGIMP_CP=1;
if ACO_EM_ELG=. then ACO_EM_ELG=0; else if ACO_EM_ELG>1 then ACO_EM_ELG=1;
if DEP_EM_VS=. then DEP_EM_VS=0;
if DP_PSYCH=. then DP_PSYCH=0; 
if DP_PCPMD=. then DP_PCPMD=0; 
if DP_APN=. then DP_APN=0;
if DP_OTHSPC=. then DP_OTHSPC=0;
if ANX_EM_VS=. then ANX_EM_VS=0;
if ANX_PSYCH=. then ANX_PSYCH=0; 
if ANX_PCPMD=. then ANX_PCPMD=0; 
if ANX_APN=. then ANX_APN=0;
if ANX_OTHSPC=. then ANX_OTHSPC=0; 
if DEP_ANX_EM_VS=. then DEP_ANX_EM_VS=0;
if DEP_ANX_PSYCH=. then DEP_ANX_PSYCH=0; 
if DEP_ANX_PCPMD=. then DEP_ANX_PCPMD=0; 
if DEP_ANX_APN=. then DEP_ANX_APN=0;
if DEP_ANX_OTHSPC=. then DEP_ANX_OTHSPC=0; 
if AD_EM_VS=. then AD_EM_VS=0;

if DSTNCT_CLIN=. then DSTNCT_CLIN=0;
if DSTNCT_PCP=. then DSTNCT_PCP=0;
if DSTNCT_APN=. then DSTNCT_APN=0;
if DSTNCT_SCP=. then DSTNCT_SCP=0;
if DSTNCT_SCP_TYP=. then DSTNCT_SCP_TYP=0;

if SCP_CRDILGY=. then SCP_CRDILGY=0;
if SCP_PUL_DIS=. then SCP_PUL_DIS=0;
if SCP_ENDCRIN=. then SCP_ENDCRIN=0;
if SCP_NEPHRLG=. then SCP_NEPHRLG=0;
if SCP_OPTHMLG=. then SCP_OPTHMLG=0;
if SCP_PODIATR=. then SCP_PODIATR=0;
if SCP_INF_DIS=. then SCP_INF_DIS=0;
if SCP_UROLOGY=. then SCP_UROLOGY=0;
if SCP_DRMTLGY=. then SCP_DRMTLGY=0;
if SCP_NEUROLG=. then SCP_NEUROLG=0;
if SCP_OTO_LRY=. then SCP_OTO_LRY=0;
if SCP_GASTROE=. then SCP_GASTROE=0;
if SCP_ALG_IMM=. then SCP_ALG_IMM=0;
if SCP_RHEUMTG=. then SCP_RHEUMTG=0;
if SCP_PRPHVSC=. then SCP_PRPHVSC=0;
if SCP_ONCOL=. then SCP_ONCOL=0;
if SCP_ORTHO=. then SCP_ORTHO=0;
if SCP_OPTOM=. then SCP_OPTOM=0;
if SCP_MLT_GRP=. then SCP_MLT_GRP=0;
if SCP_NRS_SPC=. then SCP_NRS_SPC=0;
if SCP_PSYCHIA=. then SCP_PSYCHIA=0;
if SCP_PSYCHOL=. then SCP_PSYCHOL=0;
if SCP_OTHER=. then SCP_OTHER=0;
if SCP_OTHER>=1 then SCP_OTHER=1;
run;

proc means data = sample_&YEAR. n sum mean;
var AWV_TTL EM_HOME_VS EM_VIRT_VS EM_COGIMP_CP ACO_EM_ELG DEP_EM_VS ANX_EM_VS DEP_ANX_EM_VS DP_PSYCH DP_PCPMD DP_APN DP_OTHSPC ANX_PSYCH ANX_PCPMD ANX_APN ANX_OTHSPC DEP_ANX_PSYCH DEP_ANX_PCPMD DEP_ANX_APN DEP_ANX_OTHSPC AD_EM_VS DSTNCT_CLIN DSTNCT_PCP DSTNCT_APN DSTNCT_SCP DSTNCT_SCP_TYP SCP_CRDILGY SCP_PUL_DIS SCP_ENDCRIN SCP_NEPHRLG SCP_OPTHMLG SCP_PODIATR SCP_INF_DIS SCP_UROLOGY SCP_DRMTLGY SCP_NEUROLG SCP_OTO_LRY SCP_GASTROE SCP_ALG_IMM SCP_RHEUMTG 
SCP_PRPHVSC SCP_ONCOL SCP_ORTHO SCP_OPTOM SCP_MLT_GRP SCP_NRS_SPC SCP_PSYCHIA SCP_PSYCHOL SCP_OTHER;
run;

/* 20b - Continuity of Care */
data coc_&YEAR. (keep = BASEID PRF_NPI FROM DSTNCT_TTL DSTNCT_PCP);
set temp.phy_em_&YEAR.;
if PRF_NPI ne '';
rev=substr(REV_CNTR,1,3);
DSTNCT_TTL=1;  
if HCFASPCL in ('01','08','11','37','38','50','84','97','16','42') or REV_CNTR in ('0521','0522','0525') or rev in ('096','097','098') then DSTNCT_PCP=1; else DSTNCT_PCP=0; 
/* limit HFCA spec type to GP, FP, Int Med, NP, PA, Ger Med, Prev Med, ObGyn, CN Midwif, or revenue center to FQHC, CAH, RHC*/
run;

proc sort data = coc_&YEAR. nodupkey; /* eliminating duplicate same day provider visits */
by BASEID PRF_NPI FROM;
run;

proc sort data = coc_&YEAR.;
by BASEID PRF_NPI;
run;

proc summary data = coc_&YEAR.;
by BASEID PRF_NPI;
var DSTNCT_TTL DSTNCT_PCP;
output out=byphy_&YEAR. (keep = BASEID PRF_NPI DSTNCT_TTL DSTNCT_PCP)sum=; /* summing by E&M visits at the physician level */
run;

data byphy_&YEAR. (keep = BASEID PRF_NPI ni_ttl ni_pcp N_ttl N_pcp ni_ttl_sq ni_pcp_sq);
set byphy_&YEAR.;

ni_ttl=DSTNCT_TTL;
ni_pcp=DSTNCT_PCP;
ni_ttl_sq=ni_ttl**2;
ni_pcp_sq=ni_pcp**2;
N_ttl=DSTNCT_TTL;
N_pcp=DSTNCT_PCP;

run;

proc sort data = byphy_&YEAR.;
by BASEID;
run;

proc summary data = byphy_&YEAR.; /* now summing at the member level to get the continuity stats */
by BASEID;
var ni_ttl ni_pcp N_ttl N_pcp ni_ttl_sq ni_pcp_sq;
output out=bymbr_&YEAR. (keep = BASEID ni_ttl ni_pcp N_ttl N_pcp ni_ttl_sq ni_pcp_sq)sum=; 
/* summarizing the ni_squared and N for the coc score (both total and PCP) */
run;

proc sort data = byphy_&YEAR.;
by BASEID descending ni_ttl;
run;

data nttl_&YEAR. (keep = BASEID nttl);
set byphy_&YEAR.;
by BASEID;
if FIRST.BASEID;
nttl=ni_ttl; /* identifying the ni (Total) with the most visits for the UPC score */
run;

proc sort data = byphy_&YEAR.;
by BASEID descending ni_pcp;
run;

data npcp_&YEAR. (keep = BASEID npcp);
set byphy_&YEAR.;
by BASEID;
if FIRST.BASEID;
npcp=ni_pcp; /* identifying the ni (PCPs) with the most visits for the UPC score */
run;

data coc_&YEAR. (keep = BASEID COC_SCR COC_PCP_SCR UPC_SCR UPC_PCP_SCR);
merge bymbr_&YEAR. nttl_&YEAR. npcp_&YEAR.;
by BASEID;

COC_SCR = (ni_ttl_sq - N_ttl) / (N_ttl*(N_ttl-1));
COC_PCP_SCR = (ni_pcp_sq - N_pcp) / (N_pcp*(N_pcp-1));
UPC_SCR = nttl / N_ttl;
UPC_PCP_SCR = npcp / N_pcp;

run;

Data sample_&YEAR. (keep = BASEID CL_YR PTA_MM PTB_MM FFS_MM MA_MM MA_FLAG PRVSUP PRVSUP_MED PRVSUP_RX VA_FLAG H_MEDSTA NEW_BENE ESRD DISABLED OREC_AGE_ONLY MCAID_ANY MCAID_FULL MCAID_PART MC_ENT_ST MCR_ENT_YR MCR_ENT_MM SHSV_ACO1 NXGN_ACO CPCPL_PCMH PTD_MM 
						PD_FLAG PARTD_LIS HASPTD HASPTB HASHMO MA_INS_NM PTC_STR_RT MA_SNP MA_SNP_CD MA_SNP_DE MA_SNP_FIDE MA_SNP_IN H_DOB DOB AGE_0101 DIED DOD_YRMNDY AGE64 AGE74 AGE84 AGE85 USRD MBRZP MBRCTY MBRST SSA_SCTY RUCA1 ruca_metro1 ruca_micro1
						ruca_rural1 ADI_SCR NEWENGLAND MIDWEST WESTNORTHCENTRAL SOUTHATLANTIC EASTSCENTRAL WESTSCENTRAL MOUNTAIN PACIFIC MALE FEMALE WHITE BLACK HISPANIC ASIAN NTVAM HAWPAC MIXED OTHER 
						OTHR_LANG ENG_SPK_PRB EDUNKN EDNOHS EDHSGD EDSMCL EDCLGD INCOME FPL_LTE100 FPL_101_200 FPL_GT200 MARRIED WIDOW DIV_SEP NEV_MARRY VET_STS WORKING_SR MCBS_INST MDS3_INST NE MW SO WE HSPC 
						SNF_ST SNF_DY PTA_REIMB PTB_REIMB TOT_REIMB PTD_REIMB SHSV_ACO SV_VARSTRATUM SV_VARPSU SV_XSEE_WGT CS_VARSTRATUM CS_VARPSU CS_XSEE_WGT ALZDMT_SR DPRSSN_SR
						DM_RP DM_T1 DM_T2 DM_PRE HF_RP IH_RP ANGINA_RP CD_RP HYST_RP CLN_CNCR_RP IDD_RP PSYCH_RP ADL IADL COGIMP_SR DF_LIFT_RP DF_KNEL_RP DF_WALK_RP DF_DRES_RP DF_EAT_RP OVRWT OBESE HT_CM WT_KG BAD_SR_HLTH WRS_SR_HLTH 
						TBC_NOW PREV_SMKR ALC_PRB BLDCHL_RP BLDPRS_RP FLU_RP PNEUM_RP MAMM_RP PAPS_RP PSA_RP COL_FOBT_RP COL_FOBT_RP_LT5 COL_SCOP_RP COL_SCOP_RP_LT5 dm_ftexam dm_bldtst_self dm_slfmgt_ed dm_drvs dm_hba1c dm_cntrld PHQ8 PHQ8_NR PHQ2 PHQ2_NR GAD2 GAD2_NR PHQ_DP_DIF PRB_SLEEP MED_BILS_COLLECT HC_CST_DELAY 
						TRB_GT_CARE GPS_SCR NOMD_CST MD_CARE_HLTH MD_TRVL_EASE SPC_ACCS_SAT QUAL_SAT INFO_SAT AFRD_RPLC_FOOD AFRD_EAT_HLTH NOAFRD_EAT_LESS NOAFRD_HUNGRY OWN_HOME SECT_EGHT HOME_VALU 
						LIVE_ALON N_HHLD CS_USC_XSEE_WGT SV_USC_XSEE_WGT TUP_SCR USOC USOC_PCP USOC_SPC SPC_SR PCP_SPC_COORD USOC_VS TEST_SR TEST_FLWP_SR NEED_HOME_SR HELP_HOME_SR RX_SR INFO_RX_SR HEAR_PRB HEAR_AID CS_MED_XSEE_WGT SV_MED_XSEE_WGT SR_ED_VS_IND SR_OP_VS_IND SR_MD_VS_IND SR_MD_PCP_VS_IND SR_MD_SPC_VS_IND LTC_days LTC_SNF_days HH_VS CU_HH_CLM CU_IP_ST CU_IP_DY CU_SNF_ST CU_SNF_DY CU_ED_VS CU_CLN_VS MCAID_REIMB RX_ADRD_COV_DYS RX_ADRD_PDC 
						RX_ANDP_COV_DYS RX_ANDP_PDC AD_DX_CCW AD_DX_PRYR AMI_DX CD_DX AFB_DX BRC_DX CRC_DX ENC_DX LUC_DX PRC_DX HF_DX CKD_DX DP_DX_CCW DP_DX_CCW_DT SPDP_DX_CCW DM_DX DSL_DX HTN_DX IH_DX ART_DX SRK_DX ANX_DX_CCW ANX_DX_CCW_DT SP_DX_CCW HIV_DX ID_DX_CCW PVD_DX ALC_DX DRG_DX AD_DX AD_DX_CNT DP_DX SPDP_DX ANX_DX ID_DX ID_DX_CNT AU_DX AU_DX_CNT CP_DX CP_DX_CNT AD DM HF IH CD DP	LIV_DX CAN_DX CCI_Score HCC_Score HCC_subst HCC_psych IP_ST IP_DY CV_ST 
						CV_DY DM_ACSHP CV_ACSHP ACSHP ACSHP_ACUT ACSHP_CHRN ED_VS NYU_BAL_NONER ED_VS_PSYCH ED_VS_SUBST ACSC_ED_VS FX_CLM FX_CP_CLM FLLSCRN_CLM ACP_CLM RXDOC_CLM ELDABSC_CLM HRA_CLM CRMG_CLM DP_ANX_SCRN PSYCHO_THR_VS CCM_BHI_CLM EM_TTL EM_PCP EM_APN EM_DM EM_HF EM_IH EM_CD EM_GER EM_PSYCHIA EM_NEURO EM_SCP EM_PRE_SEPT EM_FQRHC AWV_TTL EM_HOME_VS EM_VIRT_VS EM_COGIMP_CP ACO_EM_ELG DEP_EM_VS ANX_EM_VS DEP_ANX_EM_VS DP_PSYCH DP_PCPMD DP_APN DP_OTHSPC ANX_PSYCH ANX_PCPMD ANX_APN ANX_OTHSPC DEP_ANX_PSYCH DEP_ANX_PCPMD DEP_ANX_APN DEP_ANX_OTHSPC AD_EM_VS DSTNCT_CLIN DSTNCT_PCP DSTNCT_APN DSTNCT_SCP DSTNCT_SCP_TYP SCP_CRDILGY SCP_PUL_DIS SCP_ENDCRIN
						SCP_NEPHRLG SCP_OPTHMLG SCP_PODIATR SCP_INF_DIS SCP_UROLOGY SCP_DRMTLGY SCP_NEUROLG SCP_OTO_LRY SCP_GASTROE SCP_ALG_IMM SCP_RHEUMTG SCP_PRPHVSC SCP_ONCOL 
						SCP_ORTHO SCP_OPTOM SCP_MLT_GRP SCP_NRS_SPC SCP_PSYCHIA SCP_PSYCHOL SCP_OTHER COC_SCR COC_PCP_SCR UPC_SCR UPC_PCP_SCR);
merge sample_&YEAR. coc_&YEAR.;
by baseid;
if CL_YR = &YEAR.;
run;

/* 20c - zip distance measures at the member to provider level */
data mbrzip_&YEAR. (keep = BASEID mbr_zpcd);
set sample_&YEAR.;
mbr_zpcd=MBRZP;
run;

proc sort data = temp.phy_em_&YEAR.; by baseid; run;
proc sort data = mbrzip_&YEAR.; by baseid; run;

data emzip_&YEAR. (keep = BASEID PRF_NPI FROM_DT mbr_zpcd prv_zpcd EM_ZP_DST em_no_prv_zp);
merge temp.phy_em_&YEAR. mbrzip_&YEAR.;
by baseid;
if PRF_NPI ne '';
prv_zpcd=input(PROVZIP, 5.);
EM_ZP_DST=zipcitydistance(mbr_zpcd, prv_zpcd);
if EM_ZP_DST = . then em_no_prv_zp=1; else em_no_prv_zp=0;  /* exclude those visits where the distance was not calculated due to a bad zip code */
run;

proc sort data = emzip_&YEAR.;
by BASEID;
run;

proc summary data = emzip_&YEAR.;
by BASEID;
where EM_ZP_DST ne .;
var EM_ZP_DST;
output out = emzip_zpdist_&YEAR. (keep = BASEID EM_ZP_DST)mean=;
run;

proc summary data = emzip_&YEAR.;
by BASEID;
var em_no_prv_zp;
output out = emzip_zpmiss_&YEAR. (keep = BASEID em_no_prv_zp)sum=;
run;

Data sample_&YEAR. (keep = BASEID CL_YR PTA_MM PTB_MM FFS_MM MA_MM MA_FLAG PRVSUP PRVSUP_MED PRVSUP_RX VA_FLAG H_MEDSTA NEW_BENE ESRD DISABLED OREC_AGE_ONLY MCAID_ANY MCAID_FULL MCAID_PART MC_ENT_ST MCR_ENT_YR MCR_ENT_MM SHSV_ACO1 NXGN_ACO CPCPL_PCMH PTD_MM 
						PD_FLAG PARTD_LIS HASPTD HASPTB HASHMO MA_INS_NM PTC_STR_RT MA_SNP MA_SNP_CD MA_SNP_DE MA_SNP_FIDE MA_SNP_IN H_DOB DOB AGE_0101 DIED DOD_YRMNDY AGE64 AGE74 AGE84 AGE85 USRD MBRZP MBRCTY MBRST SSA_SCTY RUCA1 ruca_metro1 ruca_micro1
						ruca_rural1 ADI_SCR NEWENGLAND MIDWEST WESTNORTHCENTRAL SOUTHATLANTIC EASTSCENTRAL WESTSCENTRAL MOUNTAIN PACIFIC MALE FEMALE WHITE BLACK HISPANIC ASIAN NTVAM HAWPAC MIXED OTHER 
						OTHR_LANG ENG_SPK_PRB EDUNKN EDNOHS EDHSGD EDSMCL EDCLGD INCOME FPL_LTE100 FPL_101_200 FPL_GT200 MARRIED WIDOW DIV_SEP NEV_MARRY VET_STS WORKING_SR MCBS_INST MDS3_INST NE MW SO WE HSPC 
						SNF_ST SNF_DY PTA_REIMB PTB_REIMB TOT_REIMB PTD_REIMB SHSV_ACO SV_VARSTRATUM SV_VARPSU SV_XSEE_WGT CS_VARSTRATUM CS_VARPSU CS_XSEE_WGT ALZDMT_SR DPRSSN_SR
						DM_RP DM_T1 DM_T2 DM_PRE HF_RP IH_RP ANGINA_RP CD_RP HYST_RP CLN_CNCR_RP IDD_RP PSYCH_RP ADL IADL COGIMP_SR DF_LIFT_RP DF_KNEL_RP DF_WALK_RP DF_DRES_RP DF_EAT_RP OVRWT OBESE HT_CM WT_KG BAD_SR_HLTH WRS_SR_HLTH 
						TBC_NOW PREV_SMKR ALC_PRB BLDCHL_RP BLDPRS_RP FLU_RP PNEUM_RP MAMM_RP PAPS_RP PSA_RP COL_FOBT_RP COL_FOBT_RP_LT5 COL_SCOP_RP COL_SCOP_RP_LT5 dm_ftexam dm_bldtst_self dm_slfmgt_ed dm_drvs dm_hba1c dm_cntrld PHQ8 PHQ8_NR PHQ2 PHQ2_NR GAD2 GAD2_NR PHQ_DP_DIF PRB_SLEEP MED_BILS_COLLECT HC_CST_DELAY 
						TRB_GT_CARE GPS_SCR NOMD_CST MD_CARE_HLTH MD_TRVL_EASE SPC_ACCS_SAT QUAL_SAT INFO_SAT AFRD_RPLC_FOOD AFRD_EAT_HLTH NOAFRD_EAT_LESS NOAFRD_HUNGRY OWN_HOME SECT_EGHT HOME_VALU 
						LIVE_ALON N_HHLD CS_USC_XSEE_WGT SV_USC_XSEE_WGT TUP_SCR USOC USOC_PCP USOC_SPC SPC_SR PCP_SPC_COORD USOC_VS TEST_SR TEST_FLWP_SR NEED_HOME_SR HELP_HOME_SR RX_SR INFO_RX_SR HEAR_PRB HEAR_AID CS_MED_XSEE_WGT SV_MED_XSEE_WGT SR_ED_VS_IND SR_OP_VS_IND SR_MD_VS_IND SR_MD_PCP_VS_IND SR_MD_SPC_VS_IND LTC_days LTC_SNF_days HH_VS CU_HH_CLM CU_IP_ST CU_IP_DY CU_SNF_ST CU_SNF_DY CU_ED_VS CU_CLN_VS MCAID_REIMB RX_ADRD_COV_DYS RX_ADRD_PDC 
						RX_ANDP_COV_DYS RX_ANDP_PDC AD_DX_CCW AD_DX_PRYR AMI_DX CD_DX AFB_DX BRC_DX CRC_DX ENC_DX LUC_DX PRC_DX HF_DX CKD_DX DP_DX_CCW DP_DX_CCW_DT SPDP_DX_CCW DM_DX DSL_DX HTN_DX IH_DX ART_DX SRK_DX ANX_DX_CCW ANX_DX_CCW_DT SP_DX_CCW HIV_DX ID_DX_CCW PVD_DX ALC_DX DRG_DX AD_DX AD_DX_CNT DP_DX SPDP_DX ANX_DX ID_DX ID_DX_CNT AU_DX AU_DX_CNT CP_DX CP_DX_CNT AD DM HF IH CD DP	LIV_DX CAN_DX CCI_Score HCC_Score HCC_subst HCC_psych IP_ST IP_DY CV_ST 
						CV_DY DM_ACSHP CV_ACSHP ACSHP ACSHP_ACUT ACSHP_CHRN ED_VS NYU_BAL_NONER ED_VS_PSYCH ED_VS_SUBST ACSC_ED_VS FX_CLM FX_CP_CLM FLLSCRN_CLM ACP_CLM RXDOC_CLM ELDABSC_CLM HRA_CLM CRMG_CLM DP_ANX_SCRN PSYCHO_THR_VS CCM_BHI_CLM EM_TTL EM_PCP EM_APN EM_DM EM_HF EM_IH EM_CD EM_GER EM_PSYCHIA EM_NEURO EM_SCP EM_PRE_SEPT EM_FQRHC AWV_TTL EM_HOME_VS EM_VIRT_VS EM_COGIMP_CP ACO_EM_ELG DEP_EM_VS ANX_EM_VS DEP_ANX_EM_VS DP_PSYCH DP_PCPMD DP_APN DP_OTHSPC ANX_PSYCH ANX_PCPMD ANX_APN ANX_OTHSPC DEP_ANX_PSYCH DEP_ANX_PCPMD DEP_ANX_APN DEP_ANX_OTHSPC AD_EM_VS DSTNCT_CLIN DSTNCT_PCP DSTNCT_APN DSTNCT_SCP DSTNCT_SCP_TYP SCP_CRDILGY SCP_PUL_DIS SCP_ENDCRIN
						SCP_NEPHRLG SCP_OPTHMLG SCP_PODIATR SCP_INF_DIS SCP_UROLOGY SCP_DRMTLGY SCP_NEUROLG SCP_OTO_LRY SCP_GASTROE SCP_ALG_IMM SCP_RHEUMTG SCP_PRPHVSC SCP_ONCOL 
						SCP_ORTHO SCP_OPTOM SCP_MLT_GRP SCP_NRS_SPC SCP_PSYCHIA SCP_PSYCHOL SCP_OTHER COC_SCR COC_PCP_SCR UPC_SCR UPC_PCP_SCR EM_ZP_DST EM_NO_PRV_ZP);
merge sample_&YEAR. emzip_zpdist_&YEAR. emzip_zpmiss_&YEAR.;
by baseid;
if CL_YR = &YEAR.;

if EM_ZP_DST = . then EM_ZP_DST =0;
if em_no_prv_zp  =. then em_no_prv_zp = 0;
run;

/* 20d - identifying physician NPI with plurality of E&M allowed amount on claim/line items - use the latest date for E&M visits as the tiebreaker */
data eandm_&YEAR. (keep = BASEID PRF_NPI em_alw_amt);
set temp.phy_em_&YEAR.;
if HCFASPCL in ('01','08','11','38','89','50','97') /* as per MIPS guidelines - step 1 is to attribute beneficiaries based on CMS specialty codes for primary care */
or REV_CNTR in ('0521','0522','0525');
run;

proc sort data = eandm_&YEAR.;
by BASEID PRF_NPI; 
run;

proc summary data = eandm_&YEAR.;
by BASEID PRF_NPI;
var em_alw_amt;
output out=eandm_&YEAR. (keep = BASEID PRF_NPI em_alw_amt)sum=; /* summing up total E&M allowed amount by baseid and physician NPI */
run;

proc sort data = eandm_&YEAR.;
by baseid prf_npi;
run;

data eandm2_&YEAR. (keep = BASEID PRF_NPI FROM);
set temp.phy_em_&YEAR.;
if HCFASPCL in ('01','08','11','38','89','50','97') /* as per MIPS guidelines - step 1 is to attribute beneficiaries based on CMS specialty codes for primary care */
or REV_CNTR in ('0521','0522','0525');
run;

proc sort data = eandm2_&YEAR.;
by baseid prf_npi descending from;
run;

data eandm2_&YEAR. (keep = BASEID PRF_NPI FROM);
set eandm2_&YEAR.;
by baseid prf_npi;
if first.prf_npi then prf_npi_last_dt = 1; else prf_npi_last_dt = 0; /* identifying the last date each bene saw each of their individual physicians */
if prf_npi_last_dt = 1;
run;

proc sort data = eandm2_&YEAR.;
by baseid prf_npi;
run;

data eandm_&YEAR.;
merge eandm_&YEAR. eandm2_&YEAR.;
by baseid prf_npi;
run;

proc sort data = eandm_&YEAR.; /* sorting to identify the physician npi for each beneficiary who has the greatest allowed amount - and the last encounter date after that */
by baseid descending em_alw_amt descending from;
run;

data eandm_&YEAR. (keep = baseid prf_npi pcp_attrib);
set eandm_&YEAR.;
by baseid;
if first.baseid; /* keeping a list of baseids with the physician NPI with the plurality of their E&M visit allowed amount - or the last E&M visit date for tie breakers */
if prf_npi ne ''; /* deleting observations where the NPI of the physician is not identified -- I can't use these people anyway */
pcp_attrib = 1;
run;

/* now as per MIPS guidelines - step 2 is to attribute unattributed beneficiaries based on CMS specialty codes for speciality care */

/*first identifying unattributed benes */
data attributed_benes (keep = baseid attributed);
set eandm_&YEAR.;
attributed = 1;
run;

proc sort data = attributed_benes nodupkey; by baseid; run;

proc sort data = temp.phy_em_&YEAR.; by baseid; run;

data eandm_unattrib_&YEAR.;
merge temp.phy_em_&YEAR. attributed_benes;
by baseid;
if attributed =.;		/* benes not attributed in first step */
/*	CMS specialty codes for speciality care */
if HCFASPCL in ('79','03','21','06','81','07','C5','46','10','27','82','83','17','44','C3','09','90','39','13','05','35','30','93','94','36','41','72','22','37','48',
'92','70','23','99','86','12','25','84','26','29','66','C0','78','28','02','98','40','85','14','16','18','19','20','04','76','24','91','33','34','77');
run;

proc sort data = eandm_unattrib_&YEAR.;
by BASEID PRF_NPI; 
run;

proc summary data = eandm_unattrib_&YEAR.;
by BASEID PRF_NPI;
var em_alw_amt;
output out=eandm_unattrib_&YEAR. (keep = BASEID PRF_NPI em_alw_amt)sum=; /* summing up total E&M allowed amount by baseid and physician NPI */
run;

proc sort data = eandm_unattrib_&YEAR.;
by baseid prf_npi;
run;

data eandm2_&YEAR. (keep = BASEID PRF_NPI FROM);
merge temp.phy_em_&YEAR. attributed_benes;
by baseid;
if attributed =.;		/* benes not attributed in first step */
/*	CMS specialty codes for speciality care */
if HCFASPCL in ('79','03','21','06','81','07','C5','46','10','27','82','83','17','44','C3','09','90','39','13','05','35','30','93','94','36','41','72','22','37','48',
'92','70','23','99','86','12','25','84','26','29','66','C0','78','28','02','98','40','85','14','16','18','19','20','04','76','24','91','33','34','77');
run;

proc sort data = eandm2_&YEAR.;
by baseid prf_npi descending from;
run;

data eandm2_&YEAR. (keep = BASEID PRF_NPI FROM);
set eandm2_&YEAR.;
by baseid prf_npi;
if first.prf_npi then prf_npi_last_dt = 1; else prf_npi_last_dt = 0; /* identifying the last date each bene saw each of their individual physicians */
if prf_npi_last_dt = 1;
run;

proc sort data = eandm2_&YEAR.;
by baseid prf_npi;
run;

data eandm_unattrib_&YEAR.;
merge eandm_unattrib_&YEAR. eandm2_&YEAR.;
by baseid prf_npi;
run;

proc sort data = eandm_unattrib_&YEAR.; /* sorting to identify the physician npi for each beneficiary who has the greatest allowed amount - and the last encounter date after that */
by baseid descending em_alw_amt descending from;
run;

data eandm_unattrib_&YEAR. (keep = baseid prf_npi spc_attrib);
set eandm_unattrib_&YEAR.;
by baseid;
if first.baseid; /* keeping a list of baseids with the physician NPI with the plurality of their E&M visit allowed amount - or the last E&M visit date for tie breakers */
if prf_npi ne ''; /* deleting observations where the NPI of the physician is not identified -- I can't use these people anyway */
spc_attrib = 1;
run;

/*merging together the beneficiaries from the first and second stages of attribution -- these are all attributed benes */
data eandm_&YEAR. (keep = baseid prf_npi pcp_attrib spc_attrib);
set eandm_&YEAR. eandm_unattrib_&YEAR.;
if pcp_attrib = . then pcp_attrib = 0;
if spc_attrib = . then spc_attrib = 0;
run;

proc sort data = eandm_&YEAR.; 
by baseid;
run;

/* 9e - identifying whether the physician NPI with the plurality of a beneficiary's E&M claims is a geriatric specialist - as per Nikpay/NPPES def'n */
/* a - first identifying NPIs identified as such via NPPES  */
data ger_spc_&YEAR. (keep = prf_npi geriatric_spc); 
set temp.phy_ger_psychia_neuro;
if geriatric_spc=1;
run;

proc sort data = ger_spc_&YEAR. nodupkey;
by prf_npi;
run;

/* b - now merging with E&M data that shows the physician with the plurality of a beneficiary's E&M visit allowed amounts to see if that physician is a geriatrician */
proc sort data = eandm_&YEAR.;
by prf_npi;
run;

data eandm_&YEAR. (keep = baseid GER_EM_PLR PCP_EM_PLR SPC_EM_PLR);
merge eandm_&YEAR. ger_spc_&YEAR.;
by prf_npi;
if geriatric_spc=1 then GER_EM_PLR = 1; else GER_EM_PLR = 0;
if baseid ne ''; /* member is in the original file */
if pcp_attrib=1 then PCP_EM_PLR = 1; else PCP_EM_PLR = 0;
if spc_attrib=1 then SPC_EM_PLR = 1; else SPC_EM_PLR = 0;
run;

proc sort data = eandm_&YEAR.;
by baseid;
run;

Data sample_&YEAR. (keep = BASEID CL_YR PTA_MM PTB_MM FFS_MM MA_MM MA_FLAG PRVSUP PRVSUP_MED PRVSUP_RX VA_FLAG H_MEDSTA NEW_BENE ESRD DISABLED OREC_AGE_ONLY MCAID_ANY MCAID_FULL MCAID_PART MC_ENT_ST MCR_ENT_YR MCR_ENT_MM SHSV_ACO1 NXGN_ACO CPCPL_PCMH PTD_MM 
						PD_FLAG PARTD_LIS HASPTD HASPTB HASHMO MA_INS_NM PTC_STR_RT MA_SNP MA_SNP_CD MA_SNP_DE MA_SNP_FIDE MA_SNP_IN H_DOB DOB AGE_0101 DIED DOD_YRMNDY AGE64 AGE74 AGE84 AGE85 USRD MBRZP MBRCTY MBRST SSA_SCTY RUCA1 ruca_metro1 ruca_micro1
						ruca_rural1 ADI_SCR NEWENGLAND MIDWEST WESTNORTHCENTRAL SOUTHATLANTIC EASTSCENTRAL WESTSCENTRAL MOUNTAIN PACIFIC MALE FEMALE WHITE BLACK HISPANIC ASIAN NTVAM HAWPAC MIXED OTHER 
						OTHR_LANG ENG_SPK_PRB EDUNKN EDNOHS EDHSGD EDSMCL EDCLGD INCOME FPL_LTE100 FPL_101_200 FPL_GT200 MARRIED WIDOW DIV_SEP NEV_MARRY VET_STS WORKING_SR MCBS_INST MDS3_INST NE MW SO WE HSPC 
						SNF_ST SNF_DY PTA_REIMB PTB_REIMB TOT_REIMB PTD_REIMB SHSV_ACO SV_VARSTRATUM SV_VARPSU SV_XSEE_WGT CS_VARSTRATUM CS_VARPSU CS_XSEE_WGT ALZDMT_SR DPRSSN_SR
						DM_RP DM_T1 DM_T2 DM_PRE HF_RP IH_RP ANGINA_RP CD_RP HYST_RP CLN_CNCR_RP IDD_RP PSYCH_RP ADL IADL COGIMP_SR DF_LIFT_RP DF_KNEL_RP DF_WALK_RP DF_DRES_RP DF_EAT_RP OVRWT OBESE HT_CM WT_KG BAD_SR_HLTH WRS_SR_HLTH 
						TBC_NOW PREV_SMKR ALC_PRB BLDCHL_RP BLDPRS_RP FLU_RP PNEUM_RP MAMM_RP PAPS_RP PSA_RP COL_FOBT_RP COL_FOBT_RP_LT5 COL_SCOP_RP COL_SCOP_RP_LT5 dm_ftexam dm_bldtst_self dm_slfmgt_ed dm_drvs dm_hba1c dm_cntrld PHQ8 PHQ8_NR PHQ2 PHQ2_NR GAD2 GAD2_NR PHQ_DP_DIF PRB_SLEEP MED_BILS_COLLECT HC_CST_DELAY 
						TRB_GT_CARE GPS_SCR NOMD_CST MD_CARE_HLTH MD_TRVL_EASE SPC_ACCS_SAT QUAL_SAT INFO_SAT AFRD_RPLC_FOOD AFRD_EAT_HLTH NOAFRD_EAT_LESS NOAFRD_HUNGRY OWN_HOME SECT_EGHT HOME_VALU 
						LIVE_ALON N_HHLD CS_USC_XSEE_WGT SV_USC_XSEE_WGT TUP_SCR USOC USOC_PCP USOC_SPC SPC_SR PCP_SPC_COORD USOC_VS TEST_SR TEST_FLWP_SR NEED_HOME_SR HELP_HOME_SR RX_SR INFO_RX_SR HEAR_PRB HEAR_AID CS_MED_XSEE_WGT SV_MED_XSEE_WGT SR_ED_VS_IND SR_OP_VS_IND SR_MD_VS_IND SR_MD_PCP_VS_IND SR_MD_SPC_VS_IND LTC_days LTC_SNF_days HH_VS CU_HH_CLM CU_IP_ST CU_IP_DY CU_SNF_ST CU_SNF_DY CU_ED_VS CU_CLN_VS MCAID_REIMB RX_ADRD_COV_DYS RX_ADRD_PDC 
						RX_ANDP_COV_DYS RX_ANDP_PDC AD_DX_CCW AD_DX_PRYR AMI_DX CD_DX AFB_DX BRC_DX CRC_DX ENC_DX LUC_DX PRC_DX HF_DX CKD_DX DP_DX_CCW DP_DX_CCW_DT SPDP_DX_CCW DM_DX DSL_DX HTN_DX IH_DX ART_DX SRK_DX ANX_DX_CCW ANX_DX_CCW_DT SP_DX_CCW HIV_DX ID_DX_CCW PVD_DX ALC_DX DRG_DX AD_DX AD_DX_CNT DP_DX SPDP_DX ANX_DX ID_DX ID_DX_CNT AU_DX AU_DX_CNT CP_DX CP_DX_CNT AD DM HF IH CD DP	LIV_DX CAN_DX CCI_Score HCC_Score HCC_subst HCC_psych IP_ST IP_DY CV_ST 
						CV_DY DM_ACSHP CV_ACSHP ACSHP ACSHP_ACUT ACSHP_CHRN ED_VS NYU_BAL_NONER ED_VS_PSYCH ED_VS_SUBST ACSC_ED_VS FX_CLM FX_CP_CLM FLLSCRN_CLM ACP_CLM RXDOC_CLM ELDABSC_CLM HRA_CLM CRMG_CLM DP_ANX_SCRN PSYCHO_THR_VS CCM_BHI_CLM EM_TTL EM_PCP EM_APN EM_DM EM_HF EM_IH EM_CD EM_GER EM_PSYCHIA EM_NEURO EM_SCP EM_PRE_SEPT EM_FQRHC AWV_TTL EM_HOME_VS EM_VIRT_VS EM_COGIMP_CP ACO_EM_ELG DEP_EM_VS ANX_EM_VS DEP_ANX_EM_VS DP_PSYCH DP_PCPMD DP_APN DP_OTHSPC ANX_PSYCH ANX_PCPMD ANX_APN ANX_OTHSPC DEP_ANX_PSYCH DEP_ANX_PCPMD DEP_ANX_APN DEP_ANX_OTHSPC AD_EM_VS DSTNCT_CLIN DSTNCT_PCP DSTNCT_APN DSTNCT_SCP DSTNCT_SCP_TYP SCP_CRDILGY SCP_PUL_DIS SCP_ENDCRIN
						SCP_NEPHRLG SCP_OPTHMLG SCP_PODIATR SCP_INF_DIS SCP_UROLOGY SCP_DRMTLGY SCP_NEUROLG SCP_OTO_LRY SCP_GASTROE SCP_ALG_IMM SCP_RHEUMTG SCP_PRPHVSC SCP_ONCOL 
						SCP_ORTHO SCP_OPTOM SCP_MLT_GRP SCP_NRS_SPC SCP_PSYCHIA SCP_PSYCHOL SCP_OTHER COC_SCR COC_PCP_SCR UPC_SCR UPC_PCP_SCR EM_ZP_DST EM_NO_PRV_ZP GER_EM_PLR PCP_EM_PLR SPC_EM_PLR);
merge sample_&YEAR. eandm_&YEAR.;
by baseid;
if CL_YR = &YEAR.;
if GER_EM_PLR = . then GER_EM_PLR = 0;
if PCP_EM_PLR = . then PCP_EM_PLR = 0;
if SPC_EM_PLR = . then SPC_EM_PLR = 0;
run;

/* 21 - Calculating Days Alive and at Home (DAH) and Health Days Alive and at Home (HDAH) */

/* 21a - Counting DAH */

data death_days_&YEAR. (keep = baseid death_days);
set sample_&YEAR.;
from = DOD_YRMNDY; format FROM yymmdd10.;
if from ne . then thru = MDY(12,31,&YEAR); format THRU yymmdd10.; 
death_days = (thru - from) + 1;
run;

proc sort data = death_days_&YEAR. nodupkey; by baseid; run;

data other_days_&YEAR. (keep = baseid LTC_days SNF_days INP_days);
set svy_&YR.s.restmln;

if &YEAR. in (2016, 2017) then LTC_days = D_FDAYS; /* This is valid for both MA and FFS benes */
else if &YEAR.>=2018 then LTC_days = D_TFDAYS; /* This is valid for both MA and FFS benes */
SNF_days = D_SDAYS; /* This is valid for FFS benes only */
INP_days = D_IDAYS; /* This is valid for FFS benes only */

run;

proc sort data = other_days_&YEAR. nodupkey; by baseid; run;

data dah_&YEAR. (keep = baseid DAH);
merge sample_&YEAR. death_days_&YEAR. other_days_&YEAR.;
by baseid;
if SV_XSEE_WGT ne . and FFS_MM >=12; /* only counting those in SVY file with 12 FFS MM */
if death_days = . then death_days = 0;
if LTC_days = . then LTC_days = 0;
if SNF_days = . then SNF_days = 0;
if INP_days = . then INP_days = 0;
DAH = 365 - (death_days + LTC_days + SNF_days + INP_days);
if DAH <0 then DAH = 0; /* correcting for leap years so people don't show up with less than 0 days */
run;

/* 21b - Counting HDAH */

/* getting death data */
Data death_&YEAR. (keep = BASEID DOD_YRMNDY from thru type);
set sample_&YEAR.;
from = DOD_YRMNDY; format FROM yymmdd10.;
if from ne . then thru = MDY(12,31,&YEAR); format THRU yymmdd10.; 
type = 'dea';
run;

/* getting LTC facility data */
data ltc_&YEAR. (keep = baseid ltcfrom ltcthru);
set cs&YR.s.fae;
if FACDESC ne 3; /* institutionalized in LTC excluding retirement home */

REFBEGMM2 = put(REFBEGMM,z2.); format REFBEGMM2 z2.; REFBEGDD2 = put(REFBEGDD,z2.); format REFBEGDD2 z2.; REFBEGYY2 = put(REFBEGYY,z2.); format REFBEGYY2 z2.;
REFENDMM2 = put(REFENDMM,z2.); format REFENDMM2 z2.; REFENDDD2 = put(REFENDDD,z2.); format REFENDDD2 z2.; REFENDYY2 = put(REFENDYY,z2.); format REFENDYY2 z2.;

from_dt2 = REFBEGMM2||'-'||REFBEGDD2||'-'||REFBEGYY2; format ltcFROM yymmdd10.; ltcfrom = input(from_dt2,MMDDYY8.); 
thru_dt2 = REFENDMM2||'-'||REFENDDD2||'-'||REFENDYY2; format ltcTHRU yymmdd10.; ltcthru = input(thru_dt2,MMDDYY8.); 

if ltcFROM < MDY(01,01,&YEAR.) then ltcFROM = MDY(01,01,&YEAR.); /* setting Jan 1 as earliest from date */
if ltcthru=. then ltcthru = MDY(12,31,&YEAR.); /* assuming those with no thru dates stayed till end of year */
if ltcthru > MDY(12,31,&YEAR.) then ltcthru = MDY(12,31,&YEAR.); /* setting end of year as latest thru date */

if ltcFROM ne .; /* bene has record in fae file */
run;

proc datasets lib=work memtype=data;
   modify ltc_&YEAR.;
     attrib baseid label=' ';
     attrib baseid format=$8.;
run; 

proc datasets lib=work memtype=data;
   modify death_&YEAR.;
     attrib baseid label=' ';
     attrib baseid format=$8.;
run; 

proc datasets lib=work memtype=data;
   modify sample_&YEAR.;
     attrib baseid income label=' ';
     attrib baseid income format=;
run; 

proc sort data = ltc_&YEAR. nodupkey; by baseid ltcfrom ltcthru; run;

/* Getting SNF data */
Data snf_&YEAR. (keep = baseid from thru type);
set claims&YR..snf;

format FROM yymmdd10.;
format THRU yymmdd10.; 
FROM = CLM_FROM_DT;  
THRU = CLM_THRU_DT; 
if from < MDY(01,01,&YEAR.) then from = MDY(01,01,&YEAR.); 
if thru > MDY(12,31,&YEAR.) then from = MDY(12,31,&YEAR.); 

type = 'snf';
run;

proc sort data = snf_&YEAR. nodupkey; by baseid from thru; run;

/* Getting hospice data */
proc sort data = claims&YR..hsp; by baseid claimid; run;
proc sort data = claims&YR..hsprev; by baseid claimid; run;

Data hsp_&YEAR. (keep = baseid from thru type);
merge claims&YR..hsp claims&YR..hsprev;
by baseid claimid;

format FROM yymmdd10.;
format THRU yymmdd10.; 
FROM = CLM_FROM_DT;  
THRU = CLM_THRU_DT; 

if REV_CNTR not in ('0651','0652') then type = 'hsp'; /* only counting hospice claims that did NOT occur at home */
if type = 'hsp';
run;

proc sort data = hsp_&YEAR. nodupkey; by baseid from thru; run;

/* Getting inpatient stay data */
Data inp_&YEAR. (keep = baseid from thru type);
set claims&YR..inp;

format FROM yymmdd10.;
format THRU yymmdd10.; 
FROM = CLM_FROM_DT;  
THRU = CLM_THRU_DT; 

type = 'inp';
run;

proc sort data = inp_&YEAR. nodupkey; by baseid from thru; run;

/* getting ED and Obs data */
proc sort data = claims&YR..otp; by baseid claimid; run;
proc sort data = claims&YR..otprev; by baseid claimid; run;

data obs_ed_&YEAR. (keep = BASEID FROM THRU type);
merge claims&YR..otp claims&YR..otprev;
by BASEID CLAIMID;

format FROM yymmdd10.;
format THRU yymmdd10.; 
FROM = CLM_FROM_DT;  
THRU = CLM_THRU_DT; 

if REV_CNTR in ('0981','0450','0451','0452','0453','0454','0455','0456','0457','0458','0459') then type = 'edv'; /* ed visit identifier */
if REV_CNTR in ('0760', '0762') then type = 'obs';
if type in ('edv','obs');
run;

proc sort data = obs_ed_&YEAR. nodupkey; by baseid from; run; /* deleting duplicates where more than 1 ED visit / obs stay occur on same day */

/* getting home health data */

data hh_&YEAR. (keep = BASEID HH_vs);
set claims&YR..hha;
HH_vs=CLM_HHA_TOT_VISIT_CNT;
run;

proc sort data = hh_&YEAR.; by baseid; run;

proc summary data = hh_&YEAR.;
by baseid;
var HH_vs;
output out=hh_&YEAR.(keep = baseid HH_vs)sum=; /* can count all HH visits because it is impossible to get HH while in hospital, SNF, LTC */
run;

/* now setting the data together */
Data daah1_&YEAR.;
set death_&YEAR. snf_&YEAR. hsp_&YEAR. inp_&YEAR. obs_ed_&YEAR.;
if from < MDY(01,01,&YEAR.) then from = MDY(01,01,&YEAR.); 
if thru > MDY(12,31,&YEAR.) then from = MDY(12,31,&YEAR.); 
run;

proc sort data = daah1_&YEAR. nodupkey; by baseid from thru type; run;

/*counting overlap days with LTC - need to do many-to-many join on baseid so i am using sql */
proc sql noprint;
 create table combined_daah_&YEAR. as
 select *
 from daah1_&YEAR. as a 
	full outer join ltc_&YEAR. as b
 on a.baseid = b.baseid;
 quit;

Data combined_daah_&YEAR.;
set combined_daah_&YEAR.;

if from >=ltcfrom and from <= ltcthru then do;		  /* count up days dead or in hospital that overlap with LTC days */
	if thru <=ltcthru then do; 
		if type ne 'edv' then do; days3 = (thru - from) + 1; end;
		else if type = 'edv' then do; days3 = (thru - from) + 1; end;
	end;
	else if thru >ltcthru then do; 
		if type ne 'edv' then do; days3 = (ltcthru - from) + 1; end;
		if type = 'edv' then do; days3 = (ltcthru - from) + 1; end;
	end;
end;

run;

/* summing up overlapping days */
proc summary data = combined_daah_&YEAR.;
by baseid;
var days3;
output out=combined_daah_&YEAR.(keep= baseid days3)sum=;
run;

/* summing up DAAH except for LTC days */
Data daah1_&YEAR.;
set daah1_&YEAR.;
if type ne 'edv' then days1 = (thru - from) + 1; else if type = 'edv' then days1 = (thru - from) + 1; /* count up Days dead or in hospital -- counting ED days as 1 day*/															   /* count up LTC days */
run;

proc summary data = daah1_&YEAR.;
by baseid;
var days1;
output out=daah1_&YEAR.(keep = baseid days1)sum=;
run;

/* summing up LTC days */
data ltc_days_&YEAR. (keep = baseid ltcfrom ltcthru days2);
set ltc_&YEAR.;
days2 = (ltcthru - ltcfrom) + 1;
run;

proc summary data = ltc_days_&YEAR.;
by baseid;
var days2;
output out=ltc_days_&YEAR.(keep= baseid days2)sum=;
run;

/* Calculating HDAH */
data hdah_&YEAR. (keep = baseid HDAH);
merge sample_&YEAR. daah1_&YEAR ltc_days_&YEAR. combined_daah_&YEAR. hh_&YEAR.;
by baseid;
if CS_XSEE_WGT ne . and FFS_MM >=12; /* only counting those in CU file with 12 FFS MM */
if HH_vs = . then HH_vs = 0;
if days1 = . then days1 = 0;
if days2 = . then days2 = 0;
if days3 = . then days3 = 0;
HDAH = 365 - ((days1 + days2 + HH_vs) - days3); /* 365 minus days dead or in hospital or in LTC after subtracting out overlapping days with LTC */
if HDAH <0 then HDAH = 0; /* correcting for leap years so people don't show up with less than 0 days */
run;

/* merging back with main sample dataset */
Data sample_&YEAR. (keep = BASEID CL_YR PTA_MM PTB_MM FFS_MM MA_MM MA_FLAG PRVSUP PRVSUP_MED PRVSUP_RX VA_FLAG H_MEDSTA NEW_BENE ESRD DISABLED OREC_AGE_ONLY MCAID_ANY MCAID_FULL MCAID_PART MC_ENT_ST MCR_ENT_YR MCR_ENT_MM SHSV_ACO1 NXGN_ACO CPCPL_PCMH PTD_MM 
						PD_FLAG PARTD_LIS HASPTD HASPTB HASHMO MA_INS_NM PTC_STR_RT MA_SNP MA_SNP_CD MA_SNP_DE MA_SNP_FIDE MA_SNP_IN H_DOB DOB AGE_0101 DIED DOD_YRMNDY AGE64 AGE74 AGE84 AGE85 USRD MBRZP MBRCTY MBRST SSA_SCTY RUCA1 ruca_metro1 ruca_micro1
						ruca_rural1 ADI_SCR NEWENGLAND MIDWEST WESTNORTHCENTRAL SOUTHATLANTIC EASTSCENTRAL WESTSCENTRAL MOUNTAIN PACIFIC MALE FEMALE WHITE BLACK HISPANIC ASIAN NTVAM HAWPAC MIXED OTHER 
						OTHR_LANG ENG_SPK_PRB EDUNKN EDNOHS EDHSGD EDSMCL EDCLGD INCOME FPL_LTE100 FPL_101_200 FPL_GT200 MARRIED WIDOW DIV_SEP NEV_MARRY VET_STS WORKING_SR MCBS_INST MDS3_INST NE MW SO WE HSPC 
						SNF_ST SNF_DY PTA_REIMB PTB_REIMB TOT_REIMB PTD_REIMB SHSV_ACO SV_VARSTRATUM SV_VARPSU SV_XSEE_WGT CS_VARSTRATUM CS_VARPSU CS_XSEE_WGT ALZDMT_SR DPRSSN_SR
						DM_RP DM_T1 DM_T2 DM_PRE HF_RP IH_RP ANGINA_RP CD_RP HYST_RP CLN_CNCR_RP IDD_RP PSYCH_RP ADL IADL COGIMP_SR DF_LIFT_RP DF_KNEL_RP DF_WALK_RP DF_DRES_RP DF_EAT_RP OVRWT OBESE HT_CM WT_KG BAD_SR_HLTH WRS_SR_HLTH 
						TBC_NOW PREV_SMKR ALC_PRB BLDCHL_RP BLDPRS_RP FLU_RP PNEUM_RP MAMM_RP PAPS_RP PSA_RP COL_FOBT_RP COL_FOBT_RP_LT5 COL_SCOP_RP COL_SCOP_RP_LT5 dm_ftexam dm_bldtst_self dm_slfmgt_ed dm_drvs dm_hba1c dm_cntrld PHQ8 PHQ8_NR PHQ2 PHQ2_NR GAD2 GAD2_NR PHQ_DP_DIF PRB_SLEEP MED_BILS_COLLECT HC_CST_DELAY 
						TRB_GT_CARE GPS_SCR NOMD_CST MD_CARE_HLTH MD_TRVL_EASE SPC_ACCS_SAT QUAL_SAT INFO_SAT AFRD_RPLC_FOOD AFRD_EAT_HLTH NOAFRD_EAT_LESS NOAFRD_HUNGRY OWN_HOME SECT_EGHT HOME_VALU 
						LIVE_ALON N_HHLD CS_USC_XSEE_WGT SV_USC_XSEE_WGT TUP_SCR USOC USOC_PCP USOC_SPC SPC_SR PCP_SPC_COORD USOC_VS TEST_SR TEST_FLWP_SR NEED_HOME_SR HELP_HOME_SR RX_SR INFO_RX_SR HEAR_PRB HEAR_AID CS_MED_XSEE_WGT SV_MED_XSEE_WGT SR_ED_VS_IND SR_OP_VS_IND SR_MD_VS_IND SR_MD_PCP_VS_IND SR_MD_SPC_VS_IND LTC_days LTC_SNF_days HH_VS CU_HH_CLM CU_IP_ST CU_IP_DY CU_SNF_ST CU_SNF_DY CU_ED_VS CU_CLN_VS MCAID_REIMB RX_ADRD_COV_DYS RX_ADRD_PDC 
						RX_ANDP_COV_DYS RX_ANDP_PDC AD_DX_CCW AD_DX_PRYR AMI_DX CD_DX AFB_DX BRC_DX CRC_DX ENC_DX LUC_DX PRC_DX HF_DX CKD_DX DP_DX_CCW DP_DX_CCW_DT SPDP_DX_CCW DM_DX DSL_DX HTN_DX IH_DX ART_DX SRK_DX ANX_DX_CCW ANX_DX_CCW_DT SP_DX_CCW HIV_DX ID_DX_CCW PVD_DX ALC_DX DRG_DX AD_DX AD_DX_CNT DP_DX SPDP_DX ANX_DX ID_DX ID_DX_CNT AU_DX AU_DX_CNT CP_DX CP_DX_CNT AD DM HF IH CD DP	LIV_DX CAN_DX CCI_Score HCC_Score HCC_subst HCC_psych IP_ST IP_DY CV_ST 
						CV_DY DM_ACSHP CV_ACSHP ACSHP ACSHP_ACUT ACSHP_CHRN ED_VS NYU_BAL_NONER ED_VS_PSYCH ED_VS_SUBST ACSC_ED_VS FX_CLM FX_CP_CLM FLLSCRN_CLM ACP_CLM RXDOC_CLM ELDABSC_CLM HRA_CLM CRMG_CLM DP_ANX_SCRN PSYCHO_THR_VS CCM_BHI_CLM EM_TTL EM_PCP EM_APN EM_DM EM_HF EM_IH EM_CD EM_GER EM_PSYCHIA EM_NEURO EM_SCP EM_PRE_SEPT EM_FQRHC AWV_TTL EM_HOME_VS EM_VIRT_VS EM_COGIMP_CP ACO_EM_ELG DEP_EM_VS ANX_EM_VS DEP_ANX_EM_VS DP_PSYCH DP_PCPMD DP_APN DP_OTHSPC ANX_PSYCH ANX_PCPMD ANX_APN ANX_OTHSPC DEP_ANX_PSYCH DEP_ANX_PCPMD DEP_ANX_APN DEP_ANX_OTHSPC AD_EM_VS DSTNCT_CLIN DSTNCT_PCP DSTNCT_APN DSTNCT_SCP DSTNCT_SCP_TYP SCP_CRDILGY SCP_PUL_DIS SCP_ENDCRIN
						SCP_NEPHRLG SCP_OPTHMLG SCP_PODIATR SCP_INF_DIS SCP_UROLOGY SCP_DRMTLGY SCP_NEUROLG SCP_OTO_LRY SCP_GASTROE SCP_ALG_IMM SCP_RHEUMTG SCP_PRPHVSC SCP_ONCOL 
						SCP_ORTHO SCP_OPTOM SCP_MLT_GRP SCP_NRS_SPC SCP_PSYCHIA SCP_PSYCHOL SCP_OTHER COC_SCR COC_PCP_SCR UPC_SCR UPC_PCP_SCR EM_ZP_DST EM_NO_PRV_ZP GER_EM_PLR PCP_EM_PLR SPC_EM_PLR DAH HDAH NH_MDS);
merge sample_&YEAR. dah_&YEAR. hdah_&YEAR.;
by baseid;
if CL_YR = &YEAR.;
if LTC_SNF_DAYS >=100 then NH_MDS = 1; else NH_MDS = 0;
if NH_MDS=. then NH_MDS=0; /* setting = 0 for those with no records of any SNF or LTC utilization */
run;

/* I should start adding in the ahrq SoH database data in addition to the below */
/* 22 - Creating Area Level Geography Variables */
/* infiling needed variables from text files */
Data ssacty_fipscty_xwalk (keep = ssa_scty fips_scty);
	INFILE "R:\Active\local_area\ssacty_fipscty_xwalk_&YEAR..txt" 
             DLM = '09'X DSD TRUNCOVER FIRSTOBS = 2;
     INPUT ssa_scty2 :$5. fips_scty2 :$5.;
	 ssa_scty=put(input(ssa_scty2,best32.),z5.);
	 fips_scty=put(input(fips_scty2,best32.),z5.);
Run;

proc sort data = ssacty_fipscty_xwalk nodupkey; by ssa_scty fips_scty; run; /* sorting by county and checking for duplicates - should not be any */

data mbr_geo_file_&YEAR. (keep = baseid CL_YR SSA_SCTY);
set sample_&YEAR.;
run;

proc sort data = mbr_geo_file_&YEAR.; by SSA_SCTY; run;

data mbr_geo_file_&YEAR.;
merge mbr_geo_file_&YEAR. ssacty_fipscty_xwalk;
by ssa_scty;
if CL_YR = &YEAR.;
run;

proc sort data = mbr_geo_file_&YEAR.; by fips_scty; run;

proc sort data = ssacty_fipscty_xwalk; by fips_scty; run; 

/* 22a - Now bring in the county-level MSSP ACO penetration data */
/* infiling county ffs bene with e&m visits  */
Data ffs_bene_cty_&YEAR. (keep = fips_scty ffs_bene);
	INFILE "R:\Active\aco_county_data\ffs_bene_cty_&YEAR..txt" 
             DLM = '09'X DSD TRUNCOVER FIRSTOBS = 2;
	INPUT fips_scty :$5. ffs_bene :10. em_bene :10. dual_pct :6.;
if fips_scty ne '';
if ffs_bene >0; /* the county must have at least 1 FFS bene */
run;

proc sort data = ffs_bene_cty_&YEAR. nodupkey; by fips_scty; run;

/* infiling MSSP county level files */
Data mssp_bene_cty_&YEAR. (keep = ssa_scty mssp_bene);
	INFILE "R:\Active\aco_county_data\mssp_bene_cty_&YEAR..txt" 
             DLM = '09'X DSD TRUNCOVER FIRSTOBS = 2;
	INPUT ssa_st2 :2. ssa_cty2 :3. esrd_py :10. dsbld_py :10. age_dual_py :10. age_nodual_py :10. tot_py :10. tot_bene :10.;
/* adding leading zeroes back into variables  */
ssa_st=put(ssa_st2,z2.);
ssa_cty=put(ssa_cty2,z3.);
ssa_scty = ssa_st||ssa_cty;
mssp_bene = tot_bene; if mssp_bene=. then mssp_bene=0;
if ssa_scty ne '';
run;

proc sort data = mssp_bene_cty_&YEAR.; by ssa_scty; run;

proc summary data = mssp_bene_cty_&YEAR.;
by ssa_scty;
var mssp_bene;
output out=mssp_bene_cty_&YEAR. (keep = ssa_scty mssp_bene)sum=;
run;

/* calculating mssp_bene_pen by county */
data mssp_bene_pen_cty_&YEAR.;
merge ssacty_fipscty_xwalk ffs_bene_cty_&YEAR.;
by fips_scty;
if ssa_scty ne '';
if ffs_bene >0;
run;

proc sort data = mssp_bene_pen_cty_&YEAR. nodupkey; by ssa_scty; run;

data mssp_bene_pen_cty_&YEAR. (keep=fips_scty mssp_bene_pen);
merge mssp_bene_pen_cty_&YEAR. mssp_bene_cty_&YEAR.;
by ssa_scty;
if fips_scty ne '';
if ffs_bene >0;
if ffs_bene >=1 and mssp_bene=. then mssp_bene=0;
mssp_bene_pen = mssp_bene / ffs_bene; 
run;

proc sort data = mssp_bene_pen_cty_&YEAR. nodupkey; by fips_scty; run;

data mbr_geo_file_&YEAR.;
merge mbr_geo_file_&YEAR. mssp_bene_pen_cty_&YEAR.;
by fips_scty;
if CL_YR = &YEAR.;
run;

/* 22b - Now bring in the county-level Medicare Advantage market penetration data */
Data ma_county_&YEAR. (keep = fips_scty cty_mcr_enroll cty_ma_enroll cty_ma_pen);
	INFILE "R:\Active\local_area\County_Penetration_MA_&YEARt1._06.txt" 
             DLM = '09'X DSD TRUNCOVER FIRSTOBS = 2;
     INPUT fips_scty2 :$5. cty_mcr_enroll :7. cty_ma_enroll :7. cty_ma_pen :6.;
	 fips_scty=put(input(fips_scty2,best32.),z5.);
	 if cty_ma_enroll=. then do;
	 	cty_ma_enroll=10;
		cty_ma_pen = cty_ma_enroll/cty_mcr_enroll;
	end;
run;

proc sort data = ma_county_&YEAR. nodupkey; by fips_scty; run;

/* 22c - Now bring in the local area level AHRF data */
libname ahrf 'R:\Active\ahrf'; 
data ahrf_&YEAR. (keep = fips_scty PHY_SPLY_CTY PCP_SPLY_CTY MCP_SPLY_CTY GER_SPLY_CTY FRC_SPLY_CTY BED_SPLY_CTY PCT_PVRT_CTY MDN_HHLD_INC_CTY PCT_UMPL_CTY TMHI POVLT100 HIGHPOV);
set ahrf.ahrf;
if CL_YR=&YEAR.;
PHY_SPLY_CTY = PCP_SPLY_CTY + MCP_SPLY_CTY;

TMHI=MDN_HHLD_INC_CTY/1000; /* county household median income in thousands */
POVLT100=PCT_PVRT_CTY*100; /* multiplying to give a score between 0 and 100 */
if POVLT100 >20 then HIGHPOV=1; else HIGHPOV=0;
run;

proc sort data = ahrf_&YEAR.; by fips_scty; run;

data mbr_geo_file_&YEAR. (drop = CL_YR fips_scty SSA_SCTY);
merge mbr_geo_file_&YEAR. ahrf_&YEAR. ma_county_&YEAR.;
by fips_scty; 
if CL_YR = &YEAR.;
run;

proc sort data = mbr_geo_file_&YEAR.;
by BASEID;
run;

data sample_&YEAR. (keep = BASEID CL_YR PTA_MM PTB_MM FFS_MM MA_MM MA_FLAG PRVSUP PRVSUP_MED PRVSUP_RX VA_FLAG H_MEDSTA NEW_BENE ESRD DISABLED OREC_AGE_ONLY MCAID_ANY MCAID_FULL MCAID_PART MC_ENT_ST MCR_ENT_YR MCR_ENT_MM SHSV_ACO1 NXGN_ACO CPCPL_PCMH PTD_MM 
						PD_FLAG PARTD_LIS HASPTD HASPTB HASHMO MA_INS_NM PTC_STR_RT MA_SNP MA_SNP_CD MA_SNP_DE MA_SNP_FIDE MA_SNP_IN H_DOB DOB AGE_0101 DIED DOD_YRMNDY AGE64 AGE74 AGE84 AGE85 USRD MBRZP MBRCTY MBRST SSA_SCTY RUCA1 ruca_metro1 ruca_micro1
						ruca_rural1 ADI_SCR NEWENGLAND MIDWEST WESTNORTHCENTRAL SOUTHATLANTIC EASTSCENTRAL WESTSCENTRAL MOUNTAIN PACIFIC MALE FEMALE WHITE BLACK HISPANIC ASIAN NTVAM HAWPAC MIXED OTHER 
						OTHR_LANG ENG_SPK_PRB EDUNKN EDNOHS EDHSGD EDSMCL EDCLGD INCOME FPL_LTE100 FPL_101_200 FPL_GT200 MARRIED WIDOW DIV_SEP NEV_MARRY VET_STS WORKING_SR MCBS_INST MDS3_INST NE MW SO WE HSPC 
						SNF_ST SNF_DY PTA_REIMB PTB_REIMB TOT_REIMB PTD_REIMB SHSV_ACO SV_VARSTRATUM SV_VARPSU SV_XSEE_WGT CS_VARSTRATUM CS_VARPSU CS_XSEE_WGT ALZDMT_SR DPRSSN_SR
						DM_RP DM_T1 DM_T2 DM_PRE HF_RP IH_RP ANGINA_RP CD_RP HYST_RP CLN_CNCR_RP IDD_RP PSYCH_RP ADL IADL COGIMP_SR DF_LIFT_RP DF_KNEL_RP DF_WALK_RP DF_DRES_RP DF_EAT_RP OVRWT OBESE HT_CM WT_KG BAD_SR_HLTH WRS_SR_HLTH 
						TBC_NOW PREV_SMKR ALC_PRB BLDCHL_RP BLDPRS_RP FLU_RP PNEUM_RP MAMM_RP PAPS_RP PSA_RP COL_FOBT_RP COL_FOBT_RP_LT5 COL_SCOP_RP COL_SCOP_RP_LT5 dm_ftexam dm_bldtst_self dm_slfmgt_ed dm_drvs dm_hba1c dm_cntrld PHQ8 PHQ8_NR PHQ2 PHQ2_NR GAD2 GAD2_NR PHQ_DP_DIF PRB_SLEEP MED_BILS_COLLECT HC_CST_DELAY 
						TRB_GT_CARE GPS_SCR NOMD_CST MD_CARE_HLTH MD_TRVL_EASE SPC_ACCS_SAT QUAL_SAT INFO_SAT AFRD_RPLC_FOOD AFRD_EAT_HLTH NOAFRD_EAT_LESS NOAFRD_HUNGRY OWN_HOME SECT_EGHT HOME_VALU 
						LIVE_ALON N_HHLD CS_USC_XSEE_WGT SV_USC_XSEE_WGT TUP_SCR USOC USOC_PCP USOC_SPC SPC_SR PCP_SPC_COORD USOC_VS TEST_SR TEST_FLWP_SR NEED_HOME_SR HELP_HOME_SR RX_SR INFO_RX_SR HEAR_PRB HEAR_AID CS_MED_XSEE_WGT SV_MED_XSEE_WGT SR_ED_VS_IND SR_OP_VS_IND SR_MD_VS_IND SR_MD_PCP_VS_IND SR_MD_SPC_VS_IND LTC_days LTC_SNF_days HH_VS CU_HH_CLM CU_IP_ST CU_IP_DY CU_SNF_ST CU_SNF_DY CU_ED_VS CU_CLN_VS MCAID_REIMB RX_ADRD_COV_DYS RX_ADRD_PDC 
						RX_ANDP_COV_DYS RX_ANDP_PDC AD_DX_CCW AD_DX_PRYR AMI_DX CD_DX AFB_DX BRC_DX CRC_DX ENC_DX LUC_DX PRC_DX HF_DX CKD_DX DP_DX_CCW DP_DX_CCW_DT SPDP_DX_CCW DM_DX DSL_DX HTN_DX IH_DX ART_DX SRK_DX ANX_DX_CCW ANX_DX_CCW_DT SP_DX_CCW HIV_DX ID_DX_CCW PVD_DX ALC_DX DRG_DX AD_DX AD_DX_CNT DP_DX SPDP_DX ANX_DX ID_DX ID_DX_CNT AU_DX AU_DX_CNT CP_DX CP_DX_CNT AD DM HF IH CD DP	LIV_DX CAN_DX CCI_Score HCC_Score HCC_subst HCC_psych IP_ST IP_DY CV_ST 
						CV_DY DM_ACSHP CV_ACSHP ACSHP ACSHP_ACUT ACSHP_CHRN ED_VS NYU_BAL_NONER ED_VS_PSYCH ED_VS_SUBST ACSC_ED_VS FX_CLM FX_CP_CLM FLLSCRN_CLM ACP_CLM RXDOC_CLM ELDABSC_CLM HRA_CLM CRMG_CLM DP_ANX_SCRN PSYCHO_THR_VS CCM_BHI_CLM EM_TTL EM_PCP EM_APN EM_DM EM_HF EM_IH EM_CD EM_GER EM_PSYCHIA EM_NEURO EM_SCP EM_PRE_SEPT EM_FQRHC AWV_TTL EM_HOME_VS EM_VIRT_VS EM_COGIMP_CP ACO_EM_ELG DEP_EM_VS ANX_EM_VS DEP_ANX_EM_VS DP_PSYCH DP_PCPMD DP_APN DP_OTHSPC ANX_PSYCH ANX_PCPMD ANX_APN ANX_OTHSPC DEP_ANX_PSYCH DEP_ANX_PCPMD DEP_ANX_APN DEP_ANX_OTHSPC AD_EM_VS DSTNCT_CLIN DSTNCT_PCP DSTNCT_APN DSTNCT_SCP DSTNCT_SCP_TYP SCP_CRDILGY SCP_PUL_DIS SCP_ENDCRIN
						SCP_NEPHRLG SCP_OPTHMLG SCP_PODIATR SCP_INF_DIS SCP_UROLOGY SCP_DRMTLGY SCP_NEUROLG SCP_OTO_LRY SCP_GASTROE SCP_ALG_IMM SCP_RHEUMTG SCP_PRPHVSC SCP_ONCOL 
						SCP_ORTHO SCP_OPTOM SCP_MLT_GRP SCP_NRS_SPC SCP_PSYCHIA SCP_PSYCHOL SCP_OTHER COC_SCR COC_PCP_SCR UPC_SCR UPC_PCP_SCR EM_ZP_DST EM_NO_PRV_ZP GER_EM_PLR PCP_EM_PLR SPC_EM_PLR DAH HDAH NH_MDS 
						PHY_SPLY_CTY PCP_SPLY_CTY MCP_SPLY_CTY GER_SPLY_CTY FRC_SPLY_CTY BED_SPLY_CTY PCT_PVRT_CTY MDN_HHLD_INC_CTY PCT_UMPL_CTY TMHI POVLT100 HIGHPOV CTY_MSSP_PEN CTY_MCR_ENROLL CTY_MA_ENROLL CTY_MA_PEN);
merge sample_&YEAR. mbr_geo_file_&YEAR.;
by baseid;
if CL_YR = &YEAR.;
CTY_MSSP_PEN=mssp_bene_pen;
run;

/* 20c - Now bring in the local area level Dartmouth HSA data */
Data ziphsa_&YEAR.;
	INFILE "R:\Active\local_area\ziphsa&YR..txt" 
             DLM = '09'X DSD TRUNCOVER FIRSTOBS = 2;
     INPUT zip :8. hsa :$5. hrr :$3.;
Run;

proc sort data = ziphsa_&YEAR. nodupkey; by zip; run; /* sorting by zip and checking for duplicates - should not be any */
proc sort data = ziphsa_&YEAR.; by hsa; run; /* sorting by hsa */

Data hsasupply_11b (drop = hsa_pop CRIT_SPLY_HSA ED_SPLY_HSA);
	INFILE "R:\Active\local_area\dartmouthsupplydata_hsa_2011_phy.txt" 
             DLM = '09'X DSD TRUNCOVER FIRSTOBS = 2;
     INPUT hsa :$5. hsa_pop :8. TOT_PHY_SPLY_HSA :5.2 PCP_SPLY_HSA :5.2 ALL_SPC_SPLY_HSA :5.2 MCP_SPLY_HSA :5.2 SRG_SPLY_HSA :5.2 
CRD_SPLY_HSA :5.2 END_SPLY_HSA :5.2 GER_SPLY_HSA :5.2 INF_SPLY_HSA :5.2 NEP_SPLY_HSA :5.2 OPT_SPLY_HSA :5.2 PSY_SPLY_HSA :5.2 PUL_SPLY_HSA :5.2 CRIT_SPLY_HSA :5.2 ED_SPLY_HSA :5.2;
GEN_PHY_SPLY_HSA = PCP_SPLY_HSA + MCP_SPLY_HSA;
EDC_SPLY_HSA = CRIT_SPLY_HSA + ED_SPLY_HSA;
/*pcp_sply_tot=(PCP_SPLY_HSA*hsa_pop)/100000;
mcp_sply_tot=(MCP_SPLY_HSA*hsa_pop)/100000;
ln_hsa_pop=log(hsa_pop);
ln_pcp_sply_tot=log(pcp_sply_tot);
ln_mcp_sply_tot=log(mcp_sply_tot);*/
Run;

proc sort data = hsasupply_11b nodupkey; by hsa; run; /* sorting by hsa and checking for duplicates - should not be any */

Data hsasupply_12 (keep = hsa BED_SPLY_HSA);
	INFILE "R:\Active\local_area\dartmouthsupplydata_hsa_2012_hosp.txt" 
             DLM = '09'X DSD TRUNCOVER FIRSTOBS = 2;
     INPUT hsa :$5. hsa_pop :8. BED_HSA :5.2;
BED_SPLY_HSA=BED_HSA*100; /* changing the hospital beds metric to per 100,000 population */
Run;

proc sort data = hsasupply_12 nodupkey; by hsa; run; /* sorting by hsa and checking for duplicates - should not be any */

Data hsasupply_11;
merge hsasupply_12 hsasupply_11b;
by hsa;
run;

data hsa_link (drop=zip);
merge hsasupply_11 ziphsa_&YEAR.;
by hsa;
if zip ne .; /* zip code must be in file */
if hsa ne ''; /* hsa must be in file */
mbrzp=zip;
run;

proc sort data = hsa_link ; by mbrzp; run;

data mbr_geo_file_&YEAR. (keep = baseid CL_YR MBRZP);
set sample_&YEAR.;
run;

proc sort data = mbr_geo_file_&YEAR.; by MBRZP; run;

data mbr_geo_file_&YEAR.;
merge mbr_geo_file_&YEAR. hsa_link;
by MBRZP;
if CL_YR = &YEAR.;
run;

proc sort data = mbr_geo_file_&YEAR. ; by baseid; run;

data sample_&YEAR. (keep = BASEID CL_YR PTA_MM PTB_MM FFS_MM MA_MM MA_FLAG PRVSUP PRVSUP_MED PRVSUP_RX VA_FLAG H_MEDSTA NEW_BENE ESRD DISABLED OREC_AGE_ONLY MCAID_ANY MCAID_FULL MCAID_PART MC_ENT_ST MCR_ENT_YR MCR_ENT_MM SHSV_ACO1 NXGN_ACO CPCPL_PCMH PTD_MM 
						PD_FLAG PARTD_LIS HASPTD HASPTB HASHMO MA_INS_NM PTC_STR_RT MA_SNP MA_SNP_CD MA_SNP_DE MA_SNP_FIDE MA_SNP_IN H_DOB DOB AGE_0101 DIED DOD_YRMNDY AGE64 AGE74 AGE84 AGE85 USRD MBRZP MBRCTY MBRST SSA_SCTY RUCA1 ruca_metro1 ruca_micro1
						ruca_rural1 ADI_SCR NEWENGLAND MIDWEST WESTNORTHCENTRAL SOUTHATLANTIC EASTSCENTRAL WESTSCENTRAL MOUNTAIN PACIFIC MALE FEMALE WHITE BLACK HISPANIC ASIAN NTVAM HAWPAC MIXED OTHER 
						OTHR_LANG ENG_SPK_PRB EDUNKN EDNOHS EDHSGD EDSMCL EDCLGD INCOME FPL_LTE100 FPL_101_200 FPL_GT200 MARRIED WIDOW DIV_SEP NEV_MARRY VET_STS WORKING_SR MCBS_INST MDS3_INST NE MW SO WE HSPC 
						SNF_ST SNF_DY PTA_REIMB PTB_REIMB TOT_REIMB PTD_REIMB SHSV_ACO SV_VARSTRATUM SV_VARPSU SV_XSEE_WGT CS_VARSTRATUM CS_VARPSU CS_XSEE_WGT ALZDMT_SR DPRSSN_SR
						DM_RP DM_T1 DM_T2 DM_PRE HF_RP IH_RP ANGINA_RP CD_RP HYST_RP CLN_CNCR_RP IDD_RP PSYCH_RP ADL IADL COGIMP_SR DF_LIFT_RP DF_KNEL_RP DF_WALK_RP DF_DRES_RP DF_EAT_RP OVRWT OBESE HT_CM WT_KG BAD_SR_HLTH WRS_SR_HLTH 
						TBC_NOW PREV_SMKR ALC_PRB BLDCHL_RP BLDPRS_RP FLU_RP PNEUM_RP MAMM_RP PAPS_RP PSA_RP COL_FOBT_RP COL_FOBT_RP_LT5 COL_SCOP_RP COL_SCOP_RP_LT5 dm_ftexam dm_bldtst_self dm_slfmgt_ed dm_drvs dm_hba1c dm_cntrld PHQ8 PHQ8_NR PHQ2 PHQ2_NR GAD2 GAD2_NR PHQ_DP_DIF PRB_SLEEP MED_BILS_COLLECT HC_CST_DELAY 
						TRB_GT_CARE GPS_SCR NOMD_CST MD_CARE_HLTH MD_TRVL_EASE SPC_ACCS_SAT QUAL_SAT INFO_SAT AFRD_RPLC_FOOD AFRD_EAT_HLTH NOAFRD_EAT_LESS NOAFRD_HUNGRY OWN_HOME SECT_EGHT HOME_VALU 
						LIVE_ALON N_HHLD CS_USC_XSEE_WGT SV_USC_XSEE_WGT TUP_SCR USOC USOC_PCP USOC_SPC SPC_SR PCP_SPC_COORD USOC_VS TEST_SR TEST_FLWP_SR NEED_HOME_SR HELP_HOME_SR RX_SR INFO_RX_SR HEAR_PRB HEAR_AID CS_MED_XSEE_WGT SV_MED_XSEE_WGT SR_ED_VS_IND SR_OP_VS_IND SR_MD_VS_IND SR_MD_PCP_VS_IND SR_MD_SPC_VS_IND LTC_days LTC_SNF_days HH_VS CU_HH_CLM CU_IP_ST CU_IP_DY CU_SNF_ST CU_SNF_DY CU_ED_VS CU_CLN_VS MCAID_REIMB RX_ADRD_COV_DYS RX_ADRD_PDC 
						RX_ANDP_COV_DYS RX_ANDP_PDC AD_DX_CCW AD_DX_PRYR AMI_DX CD_DX AFB_DX BRC_DX CRC_DX ENC_DX LUC_DX PRC_DX HF_DX CKD_DX DP_DX_CCW DP_DX_CCW_DT SPDP_DX_CCW DM_DX DSL_DX HTN_DX IH_DX ART_DX SRK_DX ANX_DX_CCW ANX_DX_CCW_DT SP_DX_CCW HIV_DX ID_DX_CCW PVD_DX ALC_DX DRG_DX AD_DX AD_DX_CNT DP_DX SPDP_DX ANX_DX ID_DX ID_DX_CNT AU_DX AU_DX_CNT CP_DX CP_DX_CNT AD DM HF IH CD DP	LIV_DX CAN_DX CCI_Score HCC_Score HCC_subst HCC_psych IP_ST IP_DY CV_ST 
						CV_DY DM_ACSHP CV_ACSHP ACSHP ACSHP_ACUT ACSHP_CHRN ED_VS NYU_BAL_NONER ED_VS_PSYCH ED_VS_SUBST ACSC_ED_VS FX_CLM FX_CP_CLM FLLSCRN_CLM ACP_CLM RXDOC_CLM ELDABSC_CLM HRA_CLM CRMG_CLM DP_ANX_SCRN PSYCHO_THR_VS CCM_BHI_CLM EM_TTL EM_PCP EM_APN EM_DM EM_HF EM_IH EM_CD EM_GER EM_PSYCHIA EM_NEURO EM_SCP EM_PRE_SEPT EM_FQRHC AWV_TTL EM_HOME_VS EM_VIRT_VS EM_COGIMP_CP ACO_EM_ELG DEP_EM_VS ANX_EM_VS DEP_ANX_EM_VS DP_PSYCH DP_PCPMD DP_APN DP_OTHSPC ANX_PSYCH ANX_PCPMD ANX_APN ANX_OTHSPC DEP_ANX_PSYCH DEP_ANX_PCPMD DEP_ANX_APN DEP_ANX_OTHSPC AD_EM_VS DSTNCT_CLIN DSTNCT_PCP DSTNCT_APN DSTNCT_SCP DSTNCT_SCP_TYP SCP_CRDILGY SCP_PUL_DIS SCP_ENDCRIN
						SCP_NEPHRLG SCP_OPTHMLG SCP_PODIATR SCP_INF_DIS SCP_UROLOGY SCP_DRMTLGY SCP_NEUROLG SCP_OTO_LRY SCP_GASTROE SCP_ALG_IMM SCP_RHEUMTG SCP_PRPHVSC SCP_ONCOL 
						SCP_ORTHO SCP_OPTOM SCP_MLT_GRP SCP_NRS_SPC SCP_PSYCHIA SCP_PSYCHOL SCP_OTHER COC_SCR COC_PCP_SCR UPC_SCR UPC_PCP_SCR EM_ZP_DST EM_NO_PRV_ZP GER_EM_PLR PCP_EM_PLR SPC_EM_PLR DAH HDAH NH_MDS 
						PHY_SPLY_CTY PCP_SPLY_CTY MCP_SPLY_CTY GER_SPLY_CTY FRC_SPLY_CTY BED_SPLY_CTY PCT_PVRT_CTY MDN_HHLD_INC_CTY PCT_UMPL_CTY TMHI POVLT100 HIGHPOV CTY_MSSP_PEN CTY_MCR_ENROLL CTY_MA_ENROLL CTY_MA_PEN 
						HSA HRR TOT_PHY_SPLY_HSA PCP_SPLY_HSA MCP_SPLY_HSA GER_SPLY_HSA BED_SPLY_HSA);
merge sample_&YEAR. mbr_geo_file_&YEAR.;
by baseid;
if CL_YR = &YEAR.;
run;

/* 22d - Now bring in the local area RUCA data at the 5 digit zip code level */
/* infiling needed variables from text files */
Data ruca_codes_by_zip5 (keep = MBRZP ruca2 ruca_metro2 ruca_micro2 ruca_rural2);
	INFILE "R:\Active\local_area\ruca_codes_by_zip5.txt" 
             DLM = '09'X DSD TRUNCOVER FIRSTOBS = 2;
     INPUT MBRZP :5. ruca :$4.;
ruca2=ruca;
if ruca in ('1','1.1','2','2.1','3') then ruca_metro2 = 1; else ruca_metro2 = 0;
if ruca in ('4','4.1','5','5.1','6') then ruca_micro2 = 1; else ruca_micro2 = 0;
if ruca in ('7','7.1','7.2','8','8.1','8.2','9','10','10.1','10.2','10.3') then ruca_rural2 = 1; else ruca_rural2 = 0;

Run;

proc sort data = ruca_codes_by_zip5 nodupkey; by MBRZP; run; /* sorting by zip and checking for duplicates - should not be any */

proc sort data = sample_&YEAR.; by MBRZP; run;

data sample_&YEAR. (keep = BASEID CL_YR PTA_MM PTB_MM FFS_MM MA_MM MA_FLAG PRVSUP PRVSUP_MED PRVSUP_RX VA_FLAG H_MEDSTA NEW_BENE ESRD DISABLED OREC_AGE_ONLY MCAID_ANY MCAID_FULL MCAID_PART MC_ENT_ST MCR_ENT_YR MCR_ENT_MM SHSV_ACO1 NXGN_ACO CPCPL_PCMH PTD_MM 
						PD_FLAG PARTD_LIS HASPTD HASPTB HASHMO MA_INS_NM PTC_STR_RT MA_SNP MA_SNP_CD MA_SNP_DE MA_SNP_FIDE MA_SNP_IN H_DOB DOB AGE_0101 DIED DOD_YRMNDY AGE64 AGE74 AGE84 AGE85 USRD MBRZP MBRCTY MBRST SSA_SCTY RUCA1 ruca_metro1 ruca_micro1
						ruca_rural1 ADI_SCR NEWENGLAND MIDWEST WESTNORTHCENTRAL SOUTHATLANTIC EASTSCENTRAL WESTSCENTRAL MOUNTAIN PACIFIC MALE FEMALE WHITE BLACK HISPANIC ASIAN NTVAM HAWPAC MIXED OTHER 
						OTHR_LANG ENG_SPK_PRB EDUNKN EDNOHS EDHSGD EDSMCL EDCLGD INCOME FPL_LTE100 FPL_101_200 FPL_GT200 MARRIED WIDOW DIV_SEP NEV_MARRY VET_STS WORKING_SR MCBS_INST MDS3_INST NE MW SO WE HSPC 
						SNF_ST SNF_DY PTA_REIMB PTB_REIMB TOT_REIMB PTD_REIMB SHSV_ACO SV_VARSTRATUM SV_VARPSU SV_XSEE_WGT CS_VARSTRATUM CS_VARPSU CS_XSEE_WGT ALZDMT_SR DPRSSN_SR
						DM_RP DM_T1 DM_T2 DM_PRE HF_RP IH_RP ANGINA_RP CD_RP HYST_RP CLN_CNCR_RP IDD_RP PSYCH_RP ADL IADL COGIMP_SR DF_LIFT_RP DF_KNEL_RP DF_WALK_RP DF_DRES_RP DF_EAT_RP OVRWT OBESE HT_CM WT_KG BAD_SR_HLTH WRS_SR_HLTH 
						TBC_NOW PREV_SMKR ALC_PRB BLDCHL_RP BLDPRS_RP FLU_RP PNEUM_RP MAMM_RP PAPS_RP PSA_RP COL_FOBT_RP COL_FOBT_RP_LT5 COL_SCOP_RP COL_SCOP_RP_LT5 dm_ftexam dm_bldtst_self dm_slfmgt_ed dm_drvs dm_hba1c dm_cntrld PHQ8 PHQ8_NR PHQ2 PHQ2_NR GAD2 GAD2_NR PHQ_DP_DIF PRB_SLEEP MED_BILS_COLLECT HC_CST_DELAY 
						TRB_GT_CARE GPS_SCR NOMD_CST MD_CARE_HLTH MD_TRVL_EASE SPC_ACCS_SAT QUAL_SAT INFO_SAT AFRD_RPLC_FOOD AFRD_EAT_HLTH NOAFRD_EAT_LESS NOAFRD_HUNGRY OWN_HOME SECT_EGHT HOME_VALU 
						LIVE_ALON N_HHLD CS_USC_XSEE_WGT SV_USC_XSEE_WGT TUP_SCR USOC USOC_PCP USOC_SPC SPC_SR PCP_SPC_COORD USOC_VS TEST_SR TEST_FLWP_SR NEED_HOME_SR HELP_HOME_SR RX_SR INFO_RX_SR HEAR_PRB HEAR_AID CS_MED_XSEE_WGT SV_MED_XSEE_WGT SR_ED_VS_IND SR_OP_VS_IND SR_MD_VS_IND SR_MD_PCP_VS_IND SR_MD_SPC_VS_IND LTC_days LTC_SNF_days HH_VS CU_HH_CLM CU_IP_ST CU_IP_DY CU_SNF_ST CU_SNF_DY CU_ED_VS CU_CLN_VS MCAID_REIMB RX_ADRD_COV_DYS RX_ADRD_PDC 
						RX_ANDP_COV_DYS RX_ANDP_PDC AD_DX_CCW AD_DX_PRYR AMI_DX CD_DX AFB_DX BRC_DX CRC_DX ENC_DX LUC_DX PRC_DX HF_DX CKD_DX DP_DX_CCW DP_DX_CCW_DT SPDP_DX_CCW DM_DX DSL_DX HTN_DX IH_DX ART_DX SRK_DX ANX_DX_CCW ANX_DX_CCW_DT SP_DX_CCW HIV_DX ID_DX_CCW PVD_DX ALC_DX DRG_DX AD_DX AD_DX_CNT DP_DX SPDP_DX ANX_DX ID_DX ID_DX_CNT AU_DX AU_DX_CNT CP_DX CP_DX_CNT AD DM HF IH CD DP LIV_DX CAN_DX CCI_Score HCC_Score HCC_subst HCC_psych IP_ST IP_DY CV_ST 
						CV_DY DM_ACSHP CV_ACSHP ACSHP ACSHP_ACUT ACSHP_CHRN ED_VS NYU_BAL_NONER ED_VS_PSYCH ED_VS_SUBST ACSC_ED_VS FX_CLM FX_CP_CLM FLLSCRN_CLM ACP_CLM RXDOC_CLM ELDABSC_CLM HRA_CLM CRMG_CLM DP_ANX_SCRN PSYCHO_THR_VS CCM_BHI_CLM EM_TTL EM_PCP EM_APN EM_DM EM_HF EM_IH EM_CD EM_GER EM_PSYCHIA EM_NEURO EM_SCP EM_PRE_SEPT EM_FQRHC AWV_TTL EM_HOME_VS EM_VIRT_VS EM_COGIMP_CP ACO_EM_ELG DEP_EM_VS ANX_EM_VS DEP_ANX_EM_VS DP_PSYCH DP_PCPMD DP_APN DP_OTHSPC ANX_PSYCH ANX_PCPMD ANX_APN ANX_OTHSPC DEP_ANX_PSYCH DEP_ANX_PCPMD DEP_ANX_APN DEP_ANX_OTHSPC AD_EM_VS DSTNCT_CLIN DSTNCT_PCP DSTNCT_APN DSTNCT_SCP DSTNCT_SCP_TYP SCP_CRDILGY SCP_PUL_DIS SCP_ENDCRIN
						SCP_NEPHRLG SCP_OPTHMLG SCP_PODIATR SCP_INF_DIS SCP_UROLOGY SCP_DRMTLGY SCP_NEUROLG SCP_OTO_LRY SCP_GASTROE SCP_ALG_IMM SCP_RHEUMTG SCP_PRPHVSC SCP_ONCOL 
						SCP_ORTHO SCP_OPTOM SCP_MLT_GRP SCP_NRS_SPC SCP_PSYCHIA SCP_PSYCHOL SCP_OTHER COC_SCR COC_PCP_SCR UPC_SCR UPC_PCP_SCR EM_ZP_DST EM_NO_PRV_ZP GER_EM_PLR PCP_EM_PLR SPC_EM_PLR DAH HDAH NH_MDS 
						PHY_SPLY_CTY PCP_SPLY_CTY MCP_SPLY_CTY GER_SPLY_CTY FRC_SPLY_CTY BED_SPLY_CTY PCT_PVRT_CTY MDN_HHLD_INC_CTY PCT_UMPL_CTY TMHI POVLT100 HIGHPOV CTY_MSSP_PEN CTY_MCR_ENROLL CTY_MA_ENROLL CTY_MA_PEN 
						HSA HRR TOT_PHY_SPLY_HSA PCP_SPLY_HSA MCP_SPLY_HSA GER_SPLY_HSA BED_SPLY_HSA RUCA RUCA_METRO RUCA_MICRO RUCA_RURAL URBAN SUBURBAN LARGE_TOWN SMALL_TOWN FLYR_ERNL_PTAB);
merge sample_&YEAR. ruca_codes_by_zip5;
by MBRZP;
if CL_YR = &YEAR.;

if &YEAR. <2017 then RUCA = RUCA2; else if &YEAR. >=2017 then RUCA = RUCA1; /* MCBS measured this differently before 2017 */
if &YEAR. <2017 then RUCA_METRO = RUCA_METRO2; else if &YEAR. >=2017 then RUCA_METRO = RUCA_METRO1; /* MCBS measured this differently before 2017 */
if &YEAR. <2017 then RUCA_MICRO = RUCA_MICRO2; else if &YEAR. >=2017 then RUCA_MICRO = RUCA_MICRO1; /* MCBS measured this differently before 2017 */
if &YEAR. <2017 then RUCA_RURAL = RUCA_RURAL2; else if &YEAR. >=2017 then RUCA_RURAL = RUCA_RURAL1; /* MCBS measured this differently before 2017 */

if ruca in ('1','1.1','2','2.1','3') then URBAN = 1; else URBAN = 0;         
if ruca in ('4','4.1','5','5.1','8','8.1','8.2') then SUBURBAN = 1; else SUBURBAN = 0;
if ruca in ('6','7','7.1','7.2','9') then LARGE_TOWN = 1; else LARGE_TOWN = 0;
if ruca in ('10','10.1','10.2','10.3') then SMALL_TOWN = 1; else SMALL_TOWN = 0; 

FLYR_ERNL_PTAB=HASPTB; /* full Part A+B enrollment during the entire part of the year for which they are entitled - full year for most people*/
run;

proc sort data = sample_&YEAR.; by baseid; run; 


/* 22 - setting  up final sample analytic dataset for &YEAR. and saving to a SAS library */
libname temp 'R:\Active\mcbs\SAS\data\temp'; /* save temporary datasets here */
  
proc sql noprint;
create table temp.mcbs_sample_&YEAR.  as
select 	BASEID		as	BASEID		,
		CL_YR		as	CL_YR		,
		SV_VARSTRATUM as SV_VARSTRATUM,
		SV_VARPSU	as	SV_VARPSU	,
		SV_XSEE_WGT	as	SV_XSEE_WGT	,
		SV_MED_XSEE_WGT as SV_MED_XSEE_WGT,  /* not in 2016 */
		SV_USC_XSEE_WGT	as SV_USC_XSEE_WGT, 
		CS_VARSTRATUM as CS_VARSTRATUM,
		CS_VARPSU	as	CS_VARPSU	,
		CS_XSEE_WGT	as	CS_XSEE_WGT	,
		CS_MED_XSEE_WGT as CS_MED_XSEE_WGT, /* not in 2016 */
		CS_USC_XSEE_WGT	as CS_USC_XSEE_WGT, 
		USRD		as	USRD		,
		MC_ENT_ST	as	MC_ENT_ST	,
		MCR_ENT_YR	as	MCR_ENT_YR	,
		MCR_ENT_MM	as	MCR_ENT_MM	,
		PTA_MM		as	PTA_MM		,
		PTB_MM		as	PTB_MM		,
		FFS_MM		as	FFS_MM		,
		FLYR_ERNL_PTAB as FLYR_ERNL_PTAB,
		NEW_BENE	as	NEW_BENE	,
		MA_MM		as	MA_MM		,
		MA_FLAG		as	MA_FLAG		,
		MA_INS_NM	as	MA_INS_NM	,
		PTC_STR_RT	as	PTC_STR_RT	,
		MA_SNP		as	MA_SNP		,
		MA_SNP_CD	as	MA_SNP_CD	,
		MA_SNP_DE	as	MA_SNP_DE	,
		MA_SNP_FIDE as	MA_SNP_FIDE ,
		MA_SNP_IN	as	MA_SNP_IN	,
		DOB			as	DOB			,
		AGE_0101 	as	AGE_0101	,
		DIED		as	DIED		,
		DOD_YRMNDY	as	DOD_YRMNDY	,
		DAH			as	DAH			,
		HDAH		as	HDAH		,
		DISABLED	as	DISABLED	,
		IDD_RP		as	IDD_RP		,
		PSYCH_RP	as	PSYCH_RP	,
		ESRD		as	ESRD		,
		OREC_AGE_ONLY as OREC_AGE_ONLY		,
		MCAID_ANY	as	MCAID_ANY	, 
		MCAID_FULL	as	MCAID_FULL	,
		MCAID_PART	as	MCAID_PART	,
		PRVSUP		as	PRVSUP		,
		PRVSUP_MED	as	PRVSUP_MED	,
		PRVSUP_RX	as	PRVSUP_RX	,
		PTD_MM		as	PTD_MM		,
		PD_FLAG		as	PD_FLAG		,
		PARTD_LIS	as	PARTD_LIS	,
		VA_FLAG		as	VA_FLAG		,
		VET_STS		as	VET_STS		,
		WORKING_SR	as	WORKING_SR	,
		SHSV_ACO	as	SHSV_ACO	,
		NXGN_ACO	as	NXGN_ACO	, /* not in 2016 */
		CPCPL_PCMH  as  CPCPL_PCMH  , /* not in 2016 */
		FEMALE		as	FEMALE		,
		MALE		as	MALE		,
		WHITE		as	WHITE		,
		BLACK		as	BLACK		,
		HISPANIC	as	HISPANIC	,
		ASIAN		as	ASIAN		,
		NTVAM		as	NTVAM		,
		HAWPAC		as	HAWPAC		, 
		MIXED		as	MIXED		, 
		OTHER		as	OTHER		,
		OTHR_LANG	as	OTHR_LANG	,
		ENG_SPK_PRB	as	ENG_SPK_PRB	,
		EDUNKN		as	EDUNKN		,
		EDNOHS		as	EDNOHS		,
		EDHSGD		as	EDHSGD		,
		EDSMCL		as	EDSMCL		,
		EDCLGD		as	EDCLGD		,
		INCOME 		as	INCOME 		,
		FPL_LTE100	as	FPL_LTE100	,
		FPL_101_200	as	FPL_101_200	,
		FPL_GT200	as	FPL_GT200	,
		MARRIED		as	MARRIED		,
		WIDOW		as	WIDOW		, 
		DIV_SEP		as	DIV_SEP		, 
		NEV_MARRY	as	NEV_MARRY	,
		LIVE_ALON	as	LIVE_ALON	,
		N_HHLD		as	N_HHLD		,
		MED_BILS_COLLECT as MED_BILS_COLLECT,
		HC_CST_DELAY as HC_CST_DELAY,
		NOMD_CST	as	NOMD_CST	, 
		AFRD_RPLC_FOOD as AFRD_RPLC_FOOD,
		AFRD_EAT_HLTH as AFRD_EAT_HLTH,
		NOAFRD_EAT_LESS as NOAFRD_EAT_LESS, 
		NOAFRD_HUNGRY as NOAFRD_HUNGRY,
		OWN_HOME	as	OWN_HOME	,
		SECT_EGHT	as	SECT_EGHT	,
		HOME_VALU	as	HOME_VALU	,
		TBC_NOW		as	TBC_NOW		,
		PREV_SMKR	as	PREV_SMKR	,
		ALC_PRB		as	ALC_PRB		,
		OBESE		as	OBESE		,
		OVRWT		as	OVRWT		,  
		HT_CM		as	HT_CM		,
		WT_KG		as	WT_KG		, 
		PRB_SLEEP	as	PRB_SLEEP	, 
		CCI_Score	as	CCI_Score	,
		HCC_Score	as	HCC_Score	,
		HCC_subst	as	HCC_subst	,
		HCC_psych	as	HCC_psych	,
		ALZDMT_SR	as	ALZDMT_SR	,
		COGIMP_SR	as	COGIMP_SR	,
		DPRSSN_SR	as	DPRSSN_SR	,
		DM_RP		as	DM_RP		,
		PHQ8		as	PHQ8		,
		PHQ8_NR		as	PHQ8_NR		,
		PHQ2		as	PHQ2		,
		PHQ2_NR		as	PHQ2_NR		,
		GAD2		as	GAD2		,
		GAD2_NR		as	GAD2_NR		,
		PHQ_DP_DIF	as	PHQ_DP_DIF	,
		DM_T1		as	DM_T1		,
		DM_T2		as	DM_T2		,
		DM_PRE		as	DM_PRE		,
		HF_RP		as	HF_RP		,
		IH_RP		as	IH_RP		,
		ANGINA_RP	as	ANGINA_RP	,
		CD_RP		as	CD_RP		,
		HYST_RP		as	HYST_RP		,
		CLN_CNCR_RP	as	CLN_CNCR_RP	,
		HEAR_PRB	as	HEAR_PRB	,
		HEAR_AID	as	HEAR_AID	,
		DM_DX		as	DM_DX		,
		HF_DX		as	HF_DX		,
		IH_DX		as	IH_DX		,
		CD_DX		as	CD_DX		,
		AD_DX_CCW	as	AD_DX_CCW	,
		AD_DX_PRYR  as  AD_DX_PRYR	,
		AD_DX		as	AD_DX		,
		AD_DX_CNT   as	AD_DX_CNT	, 
		DP_DX_CCW	as	DP_DX_CCW	,
		DP_DX_CCW_DT as DP_DX_CCW_DT, 
		SPDP_DX_CCW	as	SPDP_DX_CCW	,
		DP_DX		as	DP_DX		,
		SPDP_DX		as	SPDP_DX		,
		ANX_DX_CCW	as	ANX_DX_CCW	,
		ANX_DX_CCW_DT as ANX_DX_CCW_DT,
		ANX_DX		as	ANX_DX		,
		SP_DX_CCW	as	SP_DX_CCW	,
		HTN_DX		as	HTN_DX		,
		DSL_DX		as	DSL_DX		,
		AMI_DX		as	AMI_DX		,
		SRK_DX		as	SRK_DX		,
		PVD_DX		as	PVD_DX		,
		AFB_DX		as	AFB_DX		,
		CKD_DX		as	CKD_DX		,
		CRC_DX		as	CRC_DX		,
		ENC_DX		as	ENC_DX		,
		BRC_DX		as	BRC_DX		,
		LUC_DX		as	LUC_DX		,
		PRC_DX		as	PRC_DX		,
		ART_DX		as	ART_DX		,
		ALC_DX		as	ALC_DX		,
		DRG_DX		as	DRG_DX		,
		ID_DX		as	ID_DX		,
		ID_DX_CCW	as	ID_DX_CCW	,
		ID_DX_CNT	as	ID_DX_CNT	,
		AU_DX		as	AU_DX		,
		AU_DX_CNT	as	AU_DX_CNT	,
		CP_DX		as	CP_DX		,
		CP_DX_CNT	as	CP_DX_CNT	,
		HIV_DX		as	HIV_DX		,
		LIV_DX		as	LIV_DX		,
		CAN_DX		as	CAN_DX		,
		DM			as	DM			,
		HF			as	HF			,
		IH			as	IH			,
		CD			as	CD			,
		AD			as	AD			,
		DP			as	DP			,
		BAD_SR_HLTH as	BAD_SR_HLTH	, 
		WRS_SR_HLTH	as	WRS_SR_HLTH	,
		ADL			as	ADL			,
		IADL		as	IADL		,
		DF_LIFT_RP	as	DF_LIFT_RP	,
		DF_KNEL_RP	as	DF_KNEL_RP	,
		DF_WALK_RP	as	DF_WALK_RP	,
		DF_DRES_RP	as	DF_DRES_RP	,
		DF_EAT_RP	as	DF_EAT_RP	,
 		DM_ACSHP	as	DM_ACSHP	,
		CV_ACSHP	as	CV_ACSHP	,
		ACSHP		as	ACSHP		,
		ACSHP_ACUT	as	ACSHP_ACUT	,
		ACSHP_CHRN	as	ACSHP_CHRN	,
		ED_VS		AS	ED_VS		,
		NYU_BAL_NONER as NYU_BAL_NONER,
		ED_VS_PSYCH	as	ED_VS_PSYCH	,
		ED_VS_SUBST	as	ED_VS_SUBST	,
		ACSC_ED_VS	as	ACSC_ED_VS	,
		IP_ST		as	IP_ST		,
		IP_DY		as	IP_DY		,
		CV_ST		as	CV_ST		,
		CV_DY		as	CV_DY		,
		SNF_ST		as	SNF_ST		,
		SNF_DY		as	SNF_DY		,
/*		RX_PSYCH_COV_DYS as RX_PSYCH_COV_DYS,*/
/*		RX_PSYCH_PDC as RX_PSYCH_PDC,*/
		RX_ADRD_COV_DYS as RX_ADRD_COV_DYS,
		RX_ADRD_PDC	as	RX_ADRD_PDC	,
		RX_ANDP_COV_DYS as RX_ANDP_COV_DYS,
		RX_ANDP_PDC	as	RX_ANDP_PDC	,
		SR_ED_VS_IND as SR_ED_VS_IND ,  /* not in 2016 */
		SR_OP_VS_IND as SR_OP_VS_IND ,  /* not in 2016 */
		MCAID_REIMB	as	MCAID_REIMB	,
		PTA_REIMB   as	PTA_REIMB	,
		PTB_REIMB	as	PTB_REIMB	,
		TOT_REIMB	as	TOT_REIMB	,
		PTD_REIMB	as	PTD_REIMB	,
		MCBS_INST	as	MCBS_INST	,
		MDS3_INST	as	MDS3_INST	,
		LTC_days	as	LTC_days	,
		LTC_SNF_DAYS as	LTC_SNF_DAYS,
		HSPC		as	HSPC		, 
		HH_VS		as	HH_VS		,
		CU_HH_CLM	as	CU_HH_CLM	,
		CU_IP_ST	as	CU_IP_ST	,
		CU_IP_DY	as	CU_IP_DY	,
		CU_SNF_ST	as	CU_SNF_ST	,
		CU_SNF_DY	as	CU_SNF_DY	,
		CU_ED_VS	as	CU_ED_VS	,
		CU_CLN_VS	as	CU_CLN_VS	,
		EM_TTL		as	EM_TTL		,
		EM_PCP		as	EM_PCP		,
		EM_APN		as	EM_APN		,
		EM_FQRHC	as	EM_FQRHC	,
		EM_DM		as	EM_DM		,
		EM_HF		as	EM_HF		,
		EM_IH		as	EM_IH		,
		EM_CD		as	EM_CD		,
		EM_GER		as	EM_GER		,
		EM_PSYCHIA  as  EM_PSYCHIA	,
		EM_NEURO	  as  EM_NEURO	,
		EM_SCP		as	EM_SCP		,
		EM_PRE_SEPT as	EM_PRE_SEPT	,
		FX_CLM		as	FX_CLM		,
		FX_CP_CLM	as	FX_CP_CLM	,
		FLLSCRN_CLM	as	FLLSCRN_CLM	,
		ACP_CLM		as	ACP_CLM		,
		RXDOC_CLM	as	RXDOC_CLM	,
		ELDABSC_CLM	as	ELDABSC_CLM	,
		HRA_CLM		as	HRA_CLM		,
		CRMG_CLM	as	CRMG_CLM	,
		DP_ANX_SCRN	as	DP_ANX_SCRN	,
		PSYCHO_THR_VS as PSYCHO_THR_VS,
		CCM_BHI_CLM	as	CCM_BHI_CLM	,	
		AWV_TTL		as	AWV_TTL		,
		EM_HOME_VS  as  EM_HOME_VS	,
		EM_VIRT_VS	as	EM_VIRT_VS	,
		EM_COGIMP_CP as EM_COGIMP_CP,
		ACO_EM_ELG	as	ACO_EM_ELG	,
		DEP_EM_VS	as	DEP_EM_VS	,
		DP_PSYCH	as	DP_PSYCH	,
		DP_PCPMD	as	DP_PCPMD	,
		DP_APN		as	DP_APN		,
		DP_OTHSPC	as	DP_OTHSPC	,
		ANX_EM_VS	as	ANX_EM_VS	,
		ANX_PSYCH	as	ANX_PSYCH	,
		ANX_PCPMD	as	ANX_PCPMD	,
		ANX_APN		as	ANX_APN		,
		ANX_OTHSPC	as	ANX_OTHSPC	,
		DEP_ANX_EM_VS	as	DEP_ANX_EM_VS	,
		DEP_ANX_PSYCH	as	DEP_ANX_PSYCH	,
		DEP_ANX_PCPMD	as	DEP_ANX_PCPMD	,
		DEP_ANX_APN		as	DEP_ANX_APN		,
		DEP_ANX_OTHSPC	as	DEP_ANX_OTHSPC	,
		AD_EM_VS	as	AD_EM_VS	,
		DSTNCT_CLIN	as	DSTNCT_CLIN ,
		DSTNCT_PCP	as	DSTNCT_PCP  ,
		DSTNCT_APN	as	DSTNCT_APN  ,
		DSTNCT_SCP  as	DSTNCT_SCP  ,
		DSTNCT_SCP_TYP as DSTNCT_SCP_TYP,
		SCP_CRDILGY as	SCP_CRDILGY	,
		SCP_PUL_DIS	as	SCP_PUL_DIS	,
		SCP_ENDCRIN as	SCP_ENDCRIN	,
		SCP_NEPHRLG as	SCP_NEPHRLG	,
		SCP_OPTHMLG as	SCP_OPTHMLG	,
		SCP_PODIATR as	SCP_PODIATR	,
		SCP_INF_DIS as	SCP_INF_DIS	,
		SCP_UROLOGY as	SCP_UROLOGY	,
		SCP_DRMTLGY as	SCP_DRMTLGY	,
		SCP_NEUROLG	as	SCP_NEUROLG	,
		SCP_OTO_LRY as	SCP_OTO_LRY	,
		SCP_GASTROE as	SCP_GASTROE	, 
		SCP_ALG_IMM	as	SCP_ALG_IMM	,
		SCP_RHEUMTG	as	SCP_RHEUMTG	,
		SCP_PRPHVSC	as	SCP_PRPHVSC	,
		SCP_ONCOL	as	SCP_ONCOL	,
		SCP_ORTHO	as	SCP_ORTHO	,
		SCP_OPTOM	as	SCP_OPTOM	,
		SCP_MLT_GRP	as	SCP_MLT_GRP	,
		SCP_NRS_SPC	as	SCP_NRS_SPC	,
		SCP_PSYCHIA	as	SCP_PSYCHIA	,
		SCP_PSYCHOL	as	SCP_PSYCHOL	,
		SCP_OTHER	as	SCP_OTHER	,
		EM_NO_PRV_ZP as EM_NO_PRV_ZP,
		EM_ZP_DST	as	EM_ZP_DST	,
		GER_EM_PLR  as  GER_EM_PLR	,
		PCP_EM_PLR	as	PCP_EM_PLR	,
		SPC_EM_PLR	as	SPC_EM_PLR	,
		COC_SCR		as	COC_SCR		,
		COC_PCP_SCR	as	COC_PCP_SCR	,
		UPC_SCR		as	UPC_SCR		,
		UPC_PCP_SCR as	UPC_PCP_SCR ,
		SR_MD_VS_IND as SR_MD_VS_IND ,  /* not in 2016 */
		SR_MD_PCP_VS_IND as SR_MD_PCP_VS_IND,  /* not in 2016 */
		SR_MD_SPC_VS_IND as SR_MD_SPC_VS_IND,  /* not in 2016 */
		USOC		as	USOC		,
		USOC_PCP	as	USOC_PCP	,
		USOC_SPC	as	USOC_SPC	,
		USOC_VS		as	USOC_VS		, 
		SPC_SR		as	SPC_SR		,
		PCP_SPC_COORD as PCP_SPC_COORD,
		TEST_SR		as	TEST_SR		,
		TEST_FLWP_SR as TEST_FLWP_SR,
		NEED_HOME_SR as NEED_HOME_SR,
		HELP_HOME_SR as HELP_HOME_SR,
		RX_SR		as	RX_SR		,
		INFO_RX_SR	as	INFO_RX_SR	,
		SPC_ACCS_SAT as SPC_ACCS_SAT,
		QUAL_SAT	as	QUAL_SAT	,
		INFO_SAT	as	INFO_SAT	,
		GPS_SCR		as	GPS_SCR		,
		TUP_SCR		as	TUP_SCR		,
		TRB_GT_CARE	as	TRB_GT_CARE	,
		MD_TRVL_EASE as MD_TRVL_EASE,
		MD_CARE_HLTH as MD_CARE_HLTH,
		BLDCHL_RP	as	BLDCHL_RP	,
		BLDPRS_RP	as	BLDPRS_RP	,
		FLU_RP		as	FLU_RP		,
		PNEUM_RP	as	PNEUM_RP	,
		MAMM_RP		as	MAMM_RP		, 
		PAPS_RP		as	PAPS_RP		,
		PSA_RP		as	PSA_RP		,
		COL_FOBT_RP	as	COL_FOBT_RP	,
		COL_FOBT_RP_LT5 as COL_FOBT_RP_LT5,
		COL_SCOP_RP	as	COL_SCOP_RP	,
		COL_SCOP_RP_LT5 as COL_SCOP_RP_LT5,
		MBRZP		as	MBRZP		,
		MBRCTY		as	MBRCTY		,
		MBRST		as	MBRST		,
		SSA_SCTY	as	SSA_SCTY	,  
		HSA			as	HSA			,
		HRR			as	HRR			,
		NE			as	NE			,
		MW			as	MW			,
		SO			as	SO			,
		WE			as	WE			,  
		ADI_SCR		as	ADI_SCR		,
		CTY_MCR_ENROLL as CTY_MCR_ENROLL,
		CTY_MA_ENROLL as CTY_MA_ENROLL,
		CTY_MA_PEN	as	CTY_MA_PEN	,
		CTY_MSSP_PEN as CTY_MSSP_PEN,
		PHY_SPLY_CTY as PHY_SPLY_CTY,
		PCP_SPLY_CTY as PCP_SPLY_CTY,
		MCP_SPLY_CTY as MCP_SPLY_CTY,
		GER_SPLY_CTY as GER_SPLY_CTY,
		FRC_SPLY_CTY as FRC_SPLY_CTY,
		BED_SPLY_CTY as BED_SPLY_CTY,
		PCT_PVRT_CTY as PCT_PVRT_CTY,
		MDN_HHLD_INC_CTY as MDN_HHLD_INC_CTY,
		PCT_UMPL_CTY as PCT_UMPL_CTY,
		TOT_PHY_SPLY_HSA as TOT_PHY_SPLY_HSA,
		PCP_SPLY_HSA as PCP_SPLY_HSA,
		MCP_SPLY_HSA as	MCP_SPLY_HSA,
		GER_SPLY_HSA as GER_SPLY_HSA,
		BED_SPLY_HSA as	BED_SPLY_HSA,
		RUCA		as	RUCA		,
		RUCA_METRO	as	RUCA_METRO	,
		RUCA_MICRO	as	RUCA_MICRO	,
		RUCA_RURAL	as	RUCA_RURAL	,
		dm_ftexam	as	dm_ftexam	,
		dm_bldtst_self as dm_bldtst_self,
		dm_slfmgt_ed as dm_slfmgt_ed,
		dm_drvs	as	dm_drvs	,
		dm_hba1c	as	dm_hba1c	,
		dm_cntrld	as	dm_cntrld	
from sample_&YEAR.
ORDER BY BASEID;
quit;


/*proc datasets lib=temp memtype=data;
   modify mcbs_sample_2016;
     attrib _all_ label=' ';
run;

PROC EXPORT DATA= temp.mcbs_sample_2016
            OUTFILE= "R:\Active\mcbs\Stata\data\mcbs_cohort_2016.dta" 
            DBMS=STATA LABEL REPLACE;
RUN; */
				  
   
/* 22 - Creating Final Analytic Dataset with all years merged together and exporting to Stata dataset */

libname final 'R:\Active\mcbs\SAS\data\transformed'; /* this is the path where I am saving my transformed mcbs study datasets */	
				     
data final.mcbs_cohort_2015_2019;
set temp.mcbs_sample_2015 temp.mcbs_sample_2016 temp.mcbs_sample_2017 temp.mcbs_sample_2018 temp.mcbs_sample_2019;
label BASEID=' ';
run;

proc sort data = final.mcbs_cohort_2015_2019;
by BASEID CL_YR;
run;

proc datasets lib=final memtype=data;
   modify mcbs_cohort_2015_2019;
     attrib _all_ label=' ';
run;

PROC EXPORT DATA= final.mcbs_cohort_2015_2019
            OUTFILE= "R:\Active\mcbs\Stata\data\mcbs_cohort_2015_2019.dta" 
            DBMS=STATA LABEL REPLACE;
RUN;

Data ssacty_fipscty_xwalk (keep = ssa_scty fips_scty);
	INFILE "R:\Active\local area\ssacty_fipscty_xwalk_2015.txt" 
             DLM = '09'X DSD TRUNCOVER FIRSTOBS = 2;
     INPUT ssa_scty2 :$5. fips_scty2 :$5.;
	 ssa_scty=put(input(ssa_scty2,best32.),z5.);
	 fips_scty=put(input(fips_scty2,best32.),z5.);
Run;

proc sort data = ssacty_fipscty_xwalk nodupkey; by ssa_scty fips_scty; run; /* sorting by county and checking for duplicates - should not be any */

proc sort data = ssacty_fipscty_xwalk; by fips_scty; run; 

Data ffs_bene_cty_2015 (keep = fips_scty ffs_bene);
	INFILE "R:\Active\aco county data\ffs_bene_cty_2015.txt" 
             DLM = '09'X DSD TRUNCOVER FIRSTOBS = 2;
	INPUT fips_scty :$5. ffs_bene :10. em_bene :10. dual_pct :6.;
if fips_scty ne '';
if ffs_bene >0; /* the county must have at least 1 FFS bene */
run;

proc sort data = ffs_bene_cty_2015 nodupkey; by fips_scty; run;

data mssp_pen_2015;
merge ssacty_fipscty_xwalk ffs_bene_cty_2015;
by fips_scty; 
if ffs_bene >=0;
run;

proc sort data = mssp_pen_2015; by ssa_scty; run;

/* infiling MSSP county level files */
Data mssp_bene_cty_2015 (keep = ssa_scty mssp_bene);
	INFILE "R:\Active\aco county data\mssp_bene_cty_2015.txt" 
             DLM = '09'X DSD TRUNCOVER FIRSTOBS = 2;
	INPUT ssa_st2 :2. ssa_cty2 :3. esrd_py :10. dsbld_py :10. age_dual_py :10. age_nodual_py :10. tot_py :10. tot_bene :10.;
/* adding leading zeroes back into variables  */
ssa_st=put(ssa_st2,z2.);
ssa_cty=put(ssa_cty2,z3.);
ssa_scty = ssa_st||ssa_cty;
mssp_bene = tot_bene; if mssp_bene=. then mssp_bene=0;
if ssa_scty ne '';
run;

proc sort data = mssp_bene_cty_2015; by ssa_scty; run;

proc summary data = mssp_bene_cty_2015;
by ssa_scty;
var mssp_bene;
output out=mssp_bene_cty_2015 (keep = ssa_scty mssp_bene)sum=;
run;

/* calculating mssp_bene_pen by county */
data mssp_pen_2015 (keep=ssa_scty mssp_pen_2015);
merge mssp_pen_2015 mssp_bene_cty_2015;
by ssa_scty;
if ssa_scty ne '';
if fips_scty ne '';
if ffs_bene >0;
if ffs_bene >=1 and mssp_bene=. then mssp_bene=0;
mssp_pen_2015 = mssp_bene / ffs_bene; 
run;

proc sort data = mssp_pen_2015 nodupkey; by ssa_scty; run;

proc sort data = final.mcbs_cohort_2015_2019; by ssa_scty; run;

data final.mcbs_cohort_2015_2019;
merge final.mcbs_cohort_2015_2019 mssp_pen_2015;
by ssa_scty;
if BASEID ne .;
run;

proc sort data = final.mcbs_cohort_2015_2019;
by BASEID CL_YR;
run;




/**********************************************************		END CODE		**********************************************************************************/

